-3
print'Personal information, journal and more to come'

while True:

    x = raw_input()
    if x =="personal information": 
         print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN:  , SS:'
    elif x =="journal":
       print'would you like  you open a journal or create a new one? open or create'
if x =='createfile':
           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()
elif x =='openfile':
       print'what file would you like to open' 
       y = raw_input()
       read = open(y , 'r')
       name = read.readline()
       print (name)
       break

每次我尝试运行程序时,它都会不断告诉我中断已超出循环,但我不知道我还能在哪里设置中断。还有什么是记住在循环末尾放置中断的好方法?

4

3 回答 3

5

坦率地说:你break 在循环之外。您有一个不在 while 循环内的 if 语句。

方式if x =='createfile': 是缩进的,它在while循环运行之后运行。

我猜你想重新缩进你的代码,以便它们都包含在循环中。我也将其更改为ifelif因为这在这里看起来更合适:

print 'Personal information, journal and more to come'

while True:

    x = raw_input()
    if x =="personal information": 
         print' Edward , Height: 5,10 , EYES: brown , STATE: IL TOWN:  , SS:'
    elif x =="journal":
         print'would you like  you open a journal or create a new one? open or create'
    elif x =='createfile':
         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()
    elif x =='openfile':
         print'what file would you like to open' 
         y = raw_input()
         read = open(y , 'r')
         name = read.readline()
         print (name)
         break
于 2012-08-02T19:01:57.323 回答
0

您的语句是循环之外的break语句的一部分。Python 对空格敏感。您应该将所有的and语句缩进到循环内。ifelififelif

于 2012-08-02T19:01:04.923 回答
0

你的缩进是错误的。似乎您正试图摆脱 while 循环,但您的 while 循环在

if x == 'createfile'

陈述。

您必须修复 if 和 elif 语句的缩进,以便它们位于 while 循环内,然后您的 break 语句将起作用。

于 2012-08-02T19:01:08.957 回答