python-3.x - Python:通过调用其中的函数退出while循环
问题描述
Python
def insert_tuple2(word_list2):
while True:
word_tuple = input("Write the word")
if (word_tuple,) in word_list2:
print("The word is alredy in the list!")
menu_tuple(word_list2)
else:
description_tuple = input("Write its description")
elsetuple = (word_tuple, description_tuple)
word_list2 = word_list2 + [elsetuple]
menu_tuple(word_list2)
该函数的目的是询问word_tuple
是否word_tuple
已经在列表中声明:"The word is already in the list."
应该打印并且应该要求用户在菜单功能中输入。
或者word_tuple
,description_tuple
应该保存在word_list2
菜单中,然后用户回到菜单中,其中的选项根据用户输入(整数)调用各种功能。
- 对于 insert_tuple(word_list2)
- 对于 search_tuple(word_list2)
- 对于 exit_tuple(word_list2)
问题是用户被无限地询问单词和描述输入。
解决方案
它要求input
无限,因为您使用while true:
我假设您可以为用户实现一个菜单驱动程序。
所以下面的代码可以帮助你:
def insert_tuple2(word_list2):
word_tuple = input("Write the word")
if (word_tuple,) in word_list2:
print("The word is alredy in the list!")
else:
description_tuple = input("Write its description")
elsetuple = (word_tuple, description_tuple)
word_list2 = word_list2 + [elsetuple]
menu_tuple(word_list2)
推荐阅读
- r - 总结期间出错:“closure”类型的“envir”参数无效
- reactjs - 反应js回调函数
- reactjs - useContext 与 useRef 和 useState
- google-app-engine - 谷歌语音到文本节点记录 lpcm16 流错误
- python - 这些有什么不同?TypeError:“int”对象不可迭代
- c++ - 为什么我在课程课程中收到方法“TopStudents”的错误?
- javascript - 无法从 chart.js 在同一页面上呈现两个图表
- vector - 如何将文件中的数字列表读入 Vec?
- javascript - 由于数组(Vue + Vuetify
) - git - 与我在分支中更改的文件无关的 Git 冲突