Break在循环python之外

时间:2012-07-30 22:12:29

标签: python loops if-statement break

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 

脚本一直在给我一个错误,说中断在循环之外是缩进错误吗?

2 个答案:

答案 0 :(得分:5)

是的,看看你的帖子。您的else可能与if语句的缩进级别一致。

else语句的

while语句完全不同。

答案 1 :(得分:1)

不,这不是缩进错误。你通常会“脱离”循环。 while语句中的else部分不是循环结构。如果你这样做,你会发现同样的错误

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

SyntaxError: 'break' outside loop