我试图让它从用户输入的键中打印出字典中的一个值,但是什么也没有出来。有什么想法吗
u_items=input("Enter 'list' to see a list of items, or 'Book', 'Apple', 'Toy', 'Pumpkin', 'Bowl' or 'Purse' to see the item's price: ")
u_items=u_items.title()
inventory={'Book':'18', 'Apple':'3', 'Toy':'7', 'Pumpkin':'9', 'Bowl':'5', 'Purse':'30'}
if u_items in inventory[u_items] == True:
print("A " + u_items + "costs $" + inventory[u_items])
elif u_items == 'List':
print(items(inventory))
你的if语句构造错误。应该是这样的:
测试字典中是否存在
u_items
(用户输入的键)您之前所做的是查看通过使用
u_items
索引inventory
返回的值中是否存在u_items
。因此,如果用户输入'apple'
,代码的解释如下:相关问题 更多 >
编程相关推荐