python-3.x - 使用循环和列表
问题描述
def input_list():
first_list = []
string_requested = input()
while string_requested != [""]:
new_list = first_list.append(string_requested)
想向用户询问一个字符串并添加到列表中等等,只要字符串不为空,为什么会创建一个无限循环?
解决方案
您必须读取循环内的输入才能用新输入填充列表。此外,正如已经指出的那样,append
返回None
并且不应分配。new_list
更不用说,无论如何你永远不会使用。以下将起作用:
def input_list():
new_list = []
while True:
string_requested = input()
if string_requested == "":
return new_list
new_list.append(string_requested)
推荐阅读
- ruby - appium 录屏功能
- gitlab - 如何正确扩展 .gitlab-ci.yml 中的变量?
- angular - InvalidPipeArgument:管道“AsyncPipe”的“[object Object]”
- bash - 拆分列并将新列分别附加到表中
- python - 破坏不同框架python tkinter中的标签
- google-apps-script - 无法将 Google 表格侧边栏切换到全屏
- angular - 使用 Jest 测试 Angular 服务时如何覆盖方法的实现?
- c# - 是否可以更新已设置为只读的 Word 文档而无需制作副本?
- python - 在 for 循环中使用多处理?
- python - 在 Python 3.8 中安装包 - pip 致命错误,似乎以某种方式卡在 3.6 上