3 回答
TA贡献1887条经验 获得超5个赞
试试这个:
items = {'1': '2', '3': '4', '5': '6'}
while True:
choice = input("Select your item: ")
print(choice)
if choice in items:
the_choice = items[choice]
print("You chose",the_choice)
break
print("Uh oh, I don't know about that item")
TA贡献1866条经验 获得超5个赞
只是从您的代码中忽略不是关键字
items = {'1': '2', '3': '4', '5': '6'}
choice = input("Select your item: ")
print(choice)
for choice in items:
if choice in items:
the_choice = items[choice]
print("You chose",the_choice)
break
else:
print("Uh oh, I don't know about that item")
TA贡献1995条经验 获得超2个赞
让我们来看看为什么当它不应该工作完全忽视了语法部分:
items由组成{'1': '2', '3': '4', '5': '6'}。itemssay的补码citems应包含所有字符串,但不包括items。
for not choice in items就像在说for choice in citems。对于解释器来说这没有意义,因为在这里定义这么大的集合确实是一个问题。
但是,您的问题可以通过以下方法解决:
items = {'1': '2', '3': '4', '5': '6'}
choice = input("Select your item: ")
while choice not in items:
print("Uh oh, I don't know about that item")
choice = input("Select your item: ")
the_choice = choice #assuming you want to place the value of `choice` in `the_choice` for some reason
print("You chose",the_choice)
添加回答
举报