我正在创建一个python比萨饼应用程序(是的,我知道有很多),但是,我遇到了一个需要帮助理解的场景
price = 0
toppings = {
'mushrooms': .5,
'onions': .5,
'pepperoni': 1,
'extra-cheese':1
}
pizzaSize = {
'small': 8,
'medium': 10,
'large': 12,
'x-large': 14
}
order1 = input("Welcome to the pizza store what size pizza can we get for you (small, medium, large, or x-large): " )
print(order1)
if order1 == pizzaSize.keys():
print(order1)
我知道这段代码在if order1
部分不起作用。然而,我感兴趣的是了解如何获取输入并将其与dict pizzaSize
的键相匹配
这就是为什么我要这么做。我想获取用户输入并检查它是否包含在pizzaSize
dict中
如果字符串包含在那里,我想添加与顶部的price
变量集匹配的键的值。谢谢大家!
您可以通过多种方式完成:
order1 in pizzaSize
,如果为true,则添加价格.get()
查看字典中是否存在该大小,并做出相应的反应李>pizzaSize[order1]
并在dict中不存在键时捕获异常您可能需要使用
.get()
从dict获取相应的值,如果它不存在,您将使用None
相关问题 更多 >
编程相关推荐