Break is outside the loop python

放肆的年华 提交于 2019-12-13 08:04:43

问题


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


来源:https://stackoverflow.com/questions/11730452/break-is-outside-the-loop-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!