Python 如何从while循环更改条件
我正在尝试创建一个阅读列表,使用两个函数-一个用于添加书籍,另一个用于显示书籍。添加书籍功能需要执行,直到用户确认更新完成。代码如下:Python 如何从while循环更改条件,python,Python,我正在尝试创建一个阅读列表,使用两个函数-一个用于添加书籍,另一个用于显示书籍。添加书籍功能需要执行,直到用户确认更新完成。代码如下: book_list = [] def add_books(books): book_record = {} completed = "no" while completed == "no": title = input("Enter title : ") author = input("Enter aut
book_list = []
def add_books(books):
book_record = {}
completed = "no"
while completed == "no":
title = input("Enter title : ")
author = input("Enter author : ")
year = input("Enter year of publication : ")
book_record.update({"title": title, "author": author, "year_publ": year})
books.append(book_record)
print(books)
completed = input("update completed ? yes/no")
return books
def display_books(books):
for book in books:
title = book["title"]
author = book["author"]
year = book["year_publ"]
print(f"{title}, {author}, {year}")
option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")
while option != "q" :
if option == "a":
book_list = add_books(book_list)
elif option == "d":
display_books(book_list)
else:
print("Invalid Option")
option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")
当我执行这段代码时,While循环忽略已完成的条件,并不断请求添加更多书籍,即使用户确认更新已完成
怎么了?我知道我正在尝试更新循环中已完成的内容,这可能就是原因。有哪些选择
谢谢你的帮助
谢谢和问候
Sachin您的代码一直在询问,因为在
添加书籍
完成后,选项
仍然是“a”
,并且您的“主”循环没有从用户处获得选项
的新值
始终将“是否继续?”问题作为循环的一部分
对于这类任务,我建议使用带条件中断的无止境循环(,True:
)。(在我看来)更容易理解:
这样-我添加了一个helper函数input_letter
,它返回用户响应中经过修剪的小写首字母,以便以后更容易管理):
你试过这个吗
def add_books(books):
book_record = {}
completed = "no"
while True:
title = input("Enter title : ")
author = input("Enter author : ")
year = input("Enter year of publication : ")
book_record.update({"title": title, "author": author, "year_publ": year})
books.append(book_record)
print(books)
completed = input("update completed ? yes/no")
if completed == 'yes':
break
return books
因此,问题在于
选项=input(“输入'a'添加书籍,输入'd'显示书籍,或输入'q'退出:”)
。你只要求一次选择,你需要的是一个无休止的循环,继续要求新的选择。在您的方法中,当“用户”选择“a”选项时,他/她再也不会被询问,因此选项仍保留在“a”上,因此add\u books()
函数将无休止地运行
您应将最后一部分更改为:
为True时:
选项=输入(“输入'a'以添加,输入'd'以显示书籍,或输入'q'以退出:”)
如果选项==“a”:
图书列表=添加图书(图书列表)
elif选项==“d”:
显示书籍(书籍列表)
elif选项==“q”:
退出
其他:
打印(“无效选项”)
如果您不想保留ypur代码,请使用:
另一个条件是告诉循环已完成的已更新并退出
book_list = []
def add_books(books):
book_record = {}
completed = "no"
while completed == "no":
title = input("Enter title : ")
author = input("Enter author : ")
year = input("Enter year of publication : ")
book_record.update({"title": title, "author": author, "year_publ": year})
books.append(book_record)
print(books)
completed = input("update completed ? yes/no")
if completed == "yes":
completed = completed
break
return books
def display_books(books):
for book in books:
title = book["title"]
author = book["author"]
year = book["year_publ"]
print(f"{title}, {author}, {year}")
option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")
while option != "q" :
if option == "a":
book_list = add_books(book_list)
elif option == "d":
display_books(book_list)
else:
print("Invalid Option")
option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")
当我执行这段代码时,While循环忽略已完成的条件,并不断请求添加更多书籍,即使用户确认更新已完成。这对我来说似乎很好,你做过调试吗?别忘了对帮助你的答案进行投票,当你的问题解决后,从中选出一个被接受的答案。
book_list = []
def add_books(books):
book_record = {}
completed = "no"
while completed == "no":
title = input("Enter title : ")
author = input("Enter author : ")
year = input("Enter year of publication : ")
book_record.update({"title": title, "author": author, "year_publ": year})
books.append(book_record)
print(books)
completed = input("update completed ? yes/no")
if completed == "yes":
completed = completed
break
return books
def display_books(books):
for book in books:
title = book["title"]
author = book["author"]
year = book["year_publ"]
print(f"{title}, {author}, {year}")
option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")
while option != "q" :
if option == "a":
book_list = add_books(book_list)
elif option == "d":
display_books(book_list)
else:
print("Invalid Option")
option = input("Enter 'a' to add and 'd' to display the books or 'q' to quit :")