我是一个新手程序员,但我通常可以发现这样的事情所以我认为它是一个逻辑错误而不是语法错误。所以我想知道你是否可以用新鲜的眼睛来修复错误。代码是:
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()
答案 0 :(得分:2)
您可以使用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中,规范是使用小写变量名称。)