-1
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 

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

4

2 回答 2

5

是的,看你的帖子。您的else可能意味着要使用if语句的缩进级别。

else语句的while语句做完全不同的事情。

于 2012-07-30T22:14:12.700 回答
1

不,这不是识别错误。您通常会“跳出”循环。while 语句中的 else 部分不是循环结构。如果你这样做,你会发现同样的错误

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

SyntaxError: 'break' outside loop
于 2012-07-30T22:19:20.613 回答