【问题标题】:Wont leave while loop [closed]不会离开while循环[关闭]
【发布时间】:2014-05-20 09:27:42
【问题描述】:

我是一个新手程序员,但我通常可以发现这样的事情,所以我认为这是一个逻辑错误而不是语法错误。所以我想知道你是否可以用你的新眼睛来修复错误。代码是:

database = open("database.txt", "r+")
databaselist = database.readlines()
length = len(databaselist)
for i in range (length):
    database.readline()

Continue = True
while Continue == True:
    Title = input("Enter title of book: ")
    Author = input("Enter author of book: ")
    Genre = input("Enter genre of book: ")
    Location = input("Enter the location of the book: ")
    TitleWrite = Title + "\n"
    AuthorWrite = Author + "\n"
    GenreWrite = Genre + "\n"
    LocationWrite = Location + "\n"
    database.write(str(TitleWrite))
    database.write(str(AuthorWrite))
    database.write(str(GenreWrite))
    database.write(str(LocationWrite))
    Continue2 = input("Would you like to continue? Y or N: ")
    if Continue2 == "n":
        Contine = False
        database.close()

【问题讨论】:

  • 错字Contine 是与Continue 不同的参数

标签: python debugging python-3.x error-handling


【解决方案1】:

您可以使用break 语句来跳出循环。使用它,您可以摆脱Continue 变量:

while True: # "infinite" loop that you will break out of
    title = ...
    author = ...

    response = input('Would you like to continue? Y or N: ')
    if response.lower() == 'n':
        database.close()
        break # break out of the "infinite" loop

(请注意,在 Python 中,标准是使用小写的变量名)。

【讨论】:

  • 谢谢我试试看,我相信它会奏效。 PS - 我知道 .lower() 是正常的,但我在调试时删除了它,只是没有把它放回去
猜你喜欢
  • 1970-01-01
  • 2014-02-11
  • 2016-07-07
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多