【发布时间】:2016-10-25 09:29:14
【问题描述】:
我被困在这部分代码上。这是示例文本的示例
items = [variable1, variable2, variable3]
choice = input("Input variable here: ")
if choice != items:
print("Item not found")
else:
print("Item found")
这是我想做的一个例子。我想弄清楚用户输入的内容是否是给定列表的一部分。这是python 3.5
【问题讨论】:
-
应该是
if choice not in items。如果items很大,最好将其设置为一组,即items = {} -
您好,感谢您的回复,但这不起作用,它总是输出“找不到项目”。
-
您确定要测试值的内容,而不仅仅是其名称吗?
-
如果
items中的数据是字符串,Chris 的代码将起作用。如果尼尔的回答不能解决您的问题,您应该发布一个 minimal reproducible example 来说明您的问题。
标签: python list python-3.x