Help with Break and Continue

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

Help with Break and Continue

Postby Belfast Student » Sun Aug 18, 2013 9:19 pm

Hi I've just started in Python 3.1 and can't figure out why this program won't stay open. It just closes straight away. I'm just trying to use the break and continue statements. Thanks in advance!

Code: Select all
count = 0

while True:
    count += 1
   
if count > 5:
    break
# skip number 3
if count == 3:
    continue
    print(count)

input("\n\nPress the enter key to exit.")
Belfast Student
 
Posts: 2
Joined: Sun Aug 18, 2013 9:13 pm

Re: Help with Break and Continue

Postby micseydel » Sun Aug 18, 2013 9:24 pm

You have a break outside of a loop, which is a syntax error. It looks like you're running your scripts on Windows, which means you should use an IDE such as IDLE or open a terminal and run your scripts from it rather than double clicking it.

The problem with your code is that you don't understand indentation's role in Python. Things which are indented underneath a block, such as if/for/while are inside of it. Since you dedented the part which has the break, which means you left the while block you created, it's not inside and so you get a syntax error.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1118
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help with Break and Continue

Postby Belfast Student » Mon Aug 19, 2013 12:14 pm

Ah, I understand now. Thanks! I'll have to look into indentation more.
Belfast Student
 
Posts: 2
Joined: Sun Aug 18, 2013 9:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: Letoatreides3508 and 4 guests