Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Break在循环python之外_Python_Loops_If Statement_Break - Fatal编程技术网

Break在循环python之外

Break在循环python之外,python,loops,if-statement,break,Python,Loops,If Statement,Break,脚本不断给我一个错误,说break在循环之外,缩进是否错误?是的,请查看您的帖子。您的else可能与if语句的缩进级别一致 else语句用于,而语句执行完全不同的操作。不,这不是识别错误。你通常会“打破”这个循环。while语句中的else部分不是循环构造。如果您这样做,您将发现相同的错误 while True: x = raw_input() if x =="personal information": print' Edward , Height: 5,1

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

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


else
语句用于
,而
语句执行完全不同的操作。

不,这不是识别错误。你通常会“打破”这个循环。while语句中的else部分不是循环构造。如果您这样做,您将发现相同的错误

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 

您必须修复您的契约。此外,您可能会后悔没有将open(completeName,'w')内容放入try/catch操作错误中。。。特别是如果“completeName”是根据用户输入计算的。。。这是一个缩进问题;OP显然不打算使用
while:else:
构造,这是大多数python程序员从未听说过的,在这里也不会有用。
In [12]: if True:
   ....:    break

SyntaxError: 'break' outside loop