Differences between revisions 2 and 3
Revision 2 as of 2011-12-24 14:29:00
Size: 1254
Comment:
Revision 3 as of 2012-01-06 16:36:27
Size: 2579
Editor: PaulBoddie
Comment: Tried to diagnose the problem.
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
Hello. my name is Jonmark, and I recieved the Hello! Python book as a gift in order to learn programming. I strated working, and right at the beginning, at the part where you create your first program, the basic version Hunt the Wumpus game. I copied the the written text, made the required modifications and right when I was about to finish the program (I know its to big of a word for that basic game), the command line told that at the last line (see the program code) the is an error. it said: "SyntaxError: 'break' outside loop". Hello. my name is Jonmark, and I recieved the Hello! Python book as a gift in order to learn programming. I strated working, and right at the beginning, at the part where you create your first program, the basic version Hunt the Wumpus game. I copied the the written text, made the required modifications and right when I was about to finish the program (I know its to big of a word for that basic game), the command line told that at the last line (see the program code) the is an error. it said: "!SyntaxError: 'break' outside loop".
Line 10: Line 10:
## Leave the note below so that editors can follow the instructions... ----
Line 12: Line 12:
{{{#!wiki note
When ''answering'' questions, add the CategoryAskingForHelpAnswered category when saving the page. This will move the link to this page from the questions section to the answers section on the [[Asking for Help]] page.
It sounds like the `break` statement isn't being associated with the statements inside the loop because it isn't at the right indentation level. Python uses "whitespace" - that is, spaces and tabs, which effectively leave blank space in a line of a program - to indicate groups of statements. For example:

{{{#!python
total = 0
for number in range(1, 10):
    total += number
    print total
Line 16: Line 21:
Here, lines 3 and 4 are grouped together inside the `for` loop. What may have happened to you is something like this:

{{{#!python
total = 0
for number in range(1, 10):
    total += number
print total
}}}

Here, I'm using something other than `break` to illustrate a general point. If you really wanted the `print` statement to work each time you go round the loop, then the second piece of code isn't what you want because the `print` statement on line 4 is not at the same level as the rest of the body of the loop (which is line 3).

So you need to make sure that the whitespace is exactly correct in the program you've copied from the book. You also need to be a bit careful about tabs and spaces. If you use the tab key to position the statements on each line, avoid using the space bar to do this job in the same program: sometimes the tab key produces special tab characters - not space characters - and Python treats these tab characters differently (as a certain number of spaces). You can check your program by running it with the `-t` option. For example:

{{{
python -t program.py
}}}

I hope this helps a bit. -- PaulBoddie <<DateTime(2012-01-06T17:36:25+0100)>>
Line 17: Line 41:
CategoryAskingForHelp CategoryAskingForHelp CategoryAskingForHelpAnswered

I ask for help about an error I recieved in the tutorial of Hunt the Wumpus in the Hello! Python guide book

Asking for Help: ...

Hello. my name is Jonmark, and I recieved the Hello! Python book as a gift in order to learn programming. I strated working, and right at the beginning, at the part where you create your first program, the basic version Hunt the Wumpus game. I copied the the written text, made the required modifications and right when I was about to finish the program (I know its to big of a word for that basic game), the command line told that at the last line (see the program code) the is an error. it said: "SyntaxError: 'break' outside loop". I tried for some time to modifie the statement with the meager knowledge I possess of the Python language but at the time of the writing, I held no success. can anyone aid me in this dire of times? thank you in advance, Jonmark


It sounds like the break statement isn't being associated with the statements inside the loop because it isn't at the right indentation level. Python uses "whitespace" - that is, spaces and tabs, which effectively leave blank space in a line of a program - to indicate groups of statements. For example:

   1 total = 0
   2 for number in range(1, 10):
   3     total += number
   4     print total

Here, lines 3 and 4 are grouped together inside the for loop. What may have happened to you is something like this:

   1 total = 0
   2 for number in range(1, 10):
   3     total += number
   4 print total

Here, I'm using something other than break to illustrate a general point. If you really wanted the print statement to work each time you go round the loop, then the second piece of code isn't what you want because the print statement on line 4 is not at the same level as the rest of the body of the loop (which is line 3).

So you need to make sure that the whitespace is exactly correct in the program you've copied from the book. You also need to be a bit careful about tabs and spaces. If you use the tab key to position the statements on each line, avoid using the space bar to do this job in the same program: sometimes the tab key produces special tab characters - not space characters - and Python treats these tab characters differently (as a certain number of spaces). You can check your program by running it with the -t option. For example:

python -t program.py

I hope this helps a bit. -- PaulBoddie 2012-01-06 16:36:25


CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/'break' outside loop (last edited 2012-01-06 16:36:27 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.