我在哪里把中断放在我的代码中?

Edw*_*ere -3 python loops break

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
Run Code Online (Sandbox Code Playgroud)

每次我尝试运行该程序时,它一直告诉我休息时间不在循环中,但我不知道我还能把它放在哪里.还有什么是记住在循环结束处放置休息的好方法?

Don*_*ner 5

直言不讳:你break 是在循环之外.你有一个不在while循环中的if语句.

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

我猜你想要重新编写代码,以便它们都包含在循环中.我也改变了if,elif因为这似乎更合适:

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
Run Code Online (Sandbox Code Playgroud)