Why isn't my simple FOR loop working?

This is the place for queries that don't fit in any of the other categories.

Why isn't my simple FOR loop working?

Postby tiny » Tue Oct 08, 2013 9:00 am

Hi, trying to understand why i'm not getting the expected output...

In interpreter mode, i enter this

Code: Select all
for x in range (4):
   print('Hello World')
print('I love python')


it says Syntax error!!

However, in Script Mode, when i compile the same code, it gives me what i expected....
ie.
Hello World
Hello World
Hello World
I love python

Why will it not work in interpreted mode?

thanks
tiny
Last edited by Mekire on Tue Oct 08, 2013 11:20 am, edited 1 time in total.
Reason: First post lock
tiny
 
Posts: 9
Joined: Tue Oct 08, 2013 8:55 am

Re: Why isn't my simple FOR loop working?

Postby Mekire » Tue Oct 08, 2013 11:23 am

Welcome to the forum.

How are you accessing the interpreter? Are you using an IDE (like IDLE for instance); or are you accessing it directly from command line? If you are using command line, what OS are you using?

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why isn't my simple FOR loop working?

Postby Kebap » Tue Oct 08, 2013 11:38 am

tiny wrote:it says Syntax error!!

Hi tiny, please post the complete error message (multiple lines). Thanks! :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Why isn't my simple FOR loop working?

Postby snippsat » Tue Oct 08, 2013 11:52 am

In interactive interpreter you are executing a code block when hit enter.
So you will get Syntax error because indentation get mess up for that code block.
So in interpreter you have to do it in two step..
Code: Select all
>>> for x in range (4):
   print('Hello World')
print('I love python')
SyntaxError: invalid syntax

>>> for x in range (4):
   print('Hello World')
   
Hello World
Hello World
Hello World
Hello World
>>> print('I love python')
I love python
User avatar
snippsat
 
Posts: 171
Joined: Thu Feb 21, 2013 12:04 am

Re: Why isn't my simple FOR loop working?

Postby tiny » Tue Oct 08, 2013 12:00 pm

Thanks. I'm quite surprised at that.

I would have thought that the 3 lines of code are a code block!

Code: Select all
>>> for x in range(3):
   print('Hello world')
print('I love python')
SyntaxError: invalid syntax
>>>


...i guess that the first two lines are executed only then... interesting. I never realised indentation was as vital to Python!

thanks
(Using IDLE, XP O/S)
tiny
 
Posts: 9
Joined: Tue Oct 08, 2013 8:55 am

Re: Why isn't my simple FOR loop working?

Postby Kebap » Tue Oct 08, 2013 2:31 pm

Do you by any chance copy & paste all 3 lines at the same time to the interpreter?

Because, when I insert them line by line, it works just as expected:

Code: Select all
*** Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32. ***
*** Remote Python engine  is active ***
>>> for x in range (4):
...     print('Hello World')
...     
Hello World
Hello World
Hello World
Hello World
>>> print('I love python')
I love python
>>> 


Generally, I do not succeed, when I try to copy & paste more than 1 line to the interpreter.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Why isn't my simple FOR loop working?

Postby tiny » Tue Oct 08, 2013 6:43 pm

No...

type in the following into your interpreter and you will realise it gives you a syntax error.

Code: Select all
for x in range(10):
                 print('Hello world')
print('I love Python')


...you will find out this doesn't execute.

It will only execute in SCRIPT mode.
tiny
 
Posts: 9
Joined: Tue Oct 08, 2013 8:55 am

Re: Why isn't my simple FOR loop working?

Postby Kebap » Wed Oct 09, 2013 7:23 am

See that is what I quoted in my last post. I typed exactly that. After I finish the first line, the interpreter knows, there is another line to come. That is why it prints "..." at the beginning of the line. Also, after the second line, it waits if there is another line coming. That is why I have to input an empty line at this point. Then this command block is executed. I input the last line and it is executed as well. This is how I expect it, no error at all.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Why isn't my simple FOR loop working?

Postby metulburr » Wed Oct 09, 2013 8:40 am

You have to pay attention to detail. The (...) in the interpreter indicates you are currently in an indented block of code. To exit this indented block of code you insert an empty string, by just hitting enter. At that point it gives you back your prompt (>>>). However if you dedent with the (...) prompt (your in block of code prompt), which is what you are doing in the interpreter, you will get a syntax error as your clock of code is messed up from dedentation. Thus you will get:
Code: Select all
>>> for x in range(10):
...      print('Hello world')
... print('I love Python')
  File "<stdin>", line 3
    print('I love Python')
        ^
SyntaxError: invalid syntax

The correct way to do it in the interpreter as you have done in your script is:
Code: Select all
>>> for x in range(10):
...      print('Hello world')
...
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
>>> print('I love Python')
I love Python

^^note line 3

You do not have to do this in scripts/files as python knows when you dedent, the block of code is over. This cannot be done in the interpreter as it is being executed line by line displaying the output between. the extra empty (...) tells python you are done with that block of code and to dedent back
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1420
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Why isn't my simple FOR loop working?

Postby tiny » Wed Oct 09, 2013 9:57 am

Thanks for this.

In my interpreter mode (SHELL), i do not get the ... to indicate an indentation. I am using IDLE (Python 3.3.2 Shell) not the COMMAND line.

...this poses the obvious question to me...why does python interpreter have a Command Line option and a 'Shell' option?
tiny
 
Posts: 9
Joined: Tue Oct 08, 2013 8:55 am

Re: Why isn't my simple FOR loop working?

Postby Mekire » Wed Oct 09, 2013 11:15 am

IDLE is just one of many IDEs (integrated development environments) you can use for Python. It generally comes packaged with python, but that is as far as it goes. IDLE has nothing to do with Python. Also, despite being prepackaged with Python, it actually isn't a very good IDE. The interactive prompts between IDEs may differ slightly in how things are displayed. You said you are on windows so I recommend you give Pyscripter a try.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why isn't my simple FOR loop working?

Postby tiny » Wed Oct 09, 2013 2:30 pm

Hmm...that's interesting. What has PyScripter got that IDLE hasn't i wonder?

Am i correct in stating then that the COMMAND LINE (Black screen) is 'pure' python (outside of IDE) and the 'SHELL' is Python with an IDLE interface?
tiny
 
Posts: 9
Joined: Tue Oct 08, 2013 8:55 am

Re: Why isn't my simple FOR loop working?

Postby metulburr » Wed Oct 09, 2013 2:55 pm

the command line is not python. It is an interface to your Operating System aside from the GUI (windows with x's). You can create files, delete files, invoke programs, etc. You can do anything that you can do in GUI in command line/terminal. The comand line will start you off in the path of normally
Code: Select all
C:\Windows\
or the like. From here you can change directory to pythons path and invoke the python interpreter.
Code: Select all
c:\Windows>cd ..

c:\>cd Python27

c:\Python27>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>

at this point you are in the interpreter through the command line

Now there are a ton of IDE's. Each IDE normally has an embedded python interpreter in it or a terminal. If it does not, it will usually have a plugin though to add it. An IDE is just a fancy text editor. Just a program. How each IDE handles it's embedded interpreter can be different for each one. And apparently if IDLE does not have the (...) to indicate its indentation, that is one more reason to tack on to everyone's reasoning that it is a bad IDE. AS it will only add to newcomers confusion.

This all has been described in this tutorial. I would advise to spend the time to read this tutorial all the way through.

EDIT:
I just downloaded IDLE for my windows virtual box's python, and yes it does not show the ... to indicate indentation. I would advise to find a different IDE. Or just learn to use the command line. I also updated the tutorial to advise future users of IDLE not having the ...
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1420
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], W3C [Linkcheck] and 5 guests