Break is outside the loop python

2019-09-20 19:15发布

问题:

while True:
    x = raw_input()
    if x =="personal information": 
         print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN:  , SS:'
    elif x =="journal":
         name_of_file = raw_input("What is the name of the file: ")
         completeName = "C:\\python\\" + name_of_file + ".txt"
         file1 = open(completeName , "w")
         toFile = raw_input("Write what you want into the field")
         file1.write(toFile)
         file1.close()
else:
 break 

the script keeps on giving me an error saying break is outside the loop are the indentations wrong?

回答1:

Yes, look at your post. Your else probably is meant to go with the if statement's indentation level.

else statements for while statements do entirely different things.



回答2:

No it is not an identation error. You normally "break" out of the loop. The else part in the while statement is not a loop construct. You will find the same error if you do

In [12]: if True:
   ....:    break

SyntaxError: 'break' outside loop