python - 在 for 中找到 Python .append 时不起作用,否则
问题描述
使用列表创建者为声音工作人员生成任务。根据他们接受的培训和他们能够从事的工作,创建 4 个列表并将人员添加到各种列表中。我有一份包含所有人员的基本清单。.append 适用于该列表,但对于所有带有条件的列表,名称都不会附加。
我尝试将我的 for from for str 添加到其他东西,但到目前为止没有任何效果。
my_list = []
stage_list = []
mic_list = []
all_list = []
def addto_list():
addto = input()
for str in addto:
input("Can he do stage?(y/n): ")
if input == "y":
stage_list.append(addto)
else:
break
for str in addto:
input("Can he do mic?(y/n): ")
if input == "y":
mic_list.append(addto)
else:
break
for str in addto:
input("Can he do sound?(y/n): ")
if input == "y":
all_list.append(addto)
else:
break
my_list.append(addto)
我想要的结果是当我为任何条件语句回答 y 时,名称会附加到列表中。但是当我这样做时,列表仍然显示为空白。例如我运行代码
addto_list()
Input: Jack
Can he do stage: y
can he do mic: y
can he do sound: y
print(my_list)
return: Jack
print(mic_list)
return: [] blank when it should say Jack
解决方案
您需要使input
s 单行:
my_list = []
stage_list = []
mic_list = []
all_list = []
def addto_list():
addto = input()
for str in addto:
if input("Can he do stage?(y/n): ") == "y":
stage_list.append(addto)
else:
break
for str in addto:
if input("Can he do mic?(y/n): ") == "y":
mic_list.append(addto)
else:
break
for str in addto:
if input("Can he do sound?(y/n): ") == "y":
all_list.append(addto)
else:
break
my_list.append(addto)
你的代码没有工作,因为你input
,但是你失去了对象,因为你没有分配变量,也没有在任何地方使用它,OTOHinput
是一个关键字,它是<built-in function input>
,所以它绝对不是"y"
。
推荐阅读
- windows - 自签名 SSL 证书适用于 IE 和 curl,但不适用于现代浏览器
- python - 如何在 python 中为我的数据设置日期间隔?
- postgresql - 迁移数据时如何在终端中运行 pgloader 的加载数据库命令?
- python - 为什么我没有得到我期望的输出,除非我设置一个断点?
- sql - 如何创建一个 SQL 查询来返回一个超过一周的过滤条目的列?
- html - 视频元素的宽度和高度属性是什么?
- excel - 如何防止excel在需要很长时间的已知任务上显示“无响应”
- ruby - Beginnless 日期范围不允许包含?
- r - 列出所有 shell 函数,不包括以一个或两个下划线开头的私有函数(例如“_func”或“__func”)
- reactjs - 是否有基于 Material UI 的树选择组件?