簡體   English   中英

打破Python循環

[英]Break is outside the loop python

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 

腳本不斷給我一個錯誤,說中斷不在循環內縮進是否錯誤?

是的,請查看您的帖子。 您的else可能意味着使用if語句的縮進級別。

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

不,這不是識別錯誤。 您通常會“跳出”循環。 while語句中的else部分不是循環構造。 如果執行此操作,則會發現相同的錯誤

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

SyntaxError: 'break' outside loop

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM