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
每当我尝试运行该程序时,它一直告诉我休息时间不在循环中,但我不知道我还能把它放在哪里。还有什么是记住在循环结束处放置休息的好方法?
答案 0 :(得分: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
答案 1 :(得分:0)
您的break
语句是循环之外的if
和elif
语句的一部分。 Python是空白敏感的。您应该将所有if
和elif
语句缩进到循环内。
答案 2 :(得分:0)
你的缩进是错误的。好像你试图突破while循环,但你的while循环结束于
if x == 'createfile'
语句。
你必须修复if和elif语句的缩进,以便它们在while循环中,然后你的break语句才能工作。