Python 我希望用户从inventory_price(字典)中询问物品的价格,这样它就可以打印键的值并将其作为输出
我希望用户从inventory_price(字典)中选择一个项目,如果它与字典中的键匹配,它将给出值作为输出 我尝试了一个条件语句来解决这个问题,但没有成功Python 我希望用户从inventory_price(字典)中询问物品的价格,这样它就可以打印键的值并将其作为输出,python,python-3.x,Python,Python 3.x,我希望用户从inventory_price(字典)中选择一个项目,如果它与字典中的键匹配,它将给出值作为输出 我尝试了一个条件语句来解决这个问题,但没有成功 inventory_price = { 'hair_jel' : 1200, 'hair_oil' : 500, 'facial_kit' : 2900, 'moisturising_cream' : 200, 'comb' : 150 , 'scissors' : 300, 'hai
inventory_price = {
'hair_jel' : 1200,
'hair_oil' : 500,
'facial_kit' : 2900,
'moisturising_cream' : 200,
'comb' : 150 ,
'scissors' : 300,
'hair_color' : 450
}
price = input("What is the price of {} : ".format(inventory_price.keys()))
if price == inventory_price.keys():
print(inventory_price.values())
我希望它会返回字典中键的值。- 您不会通过执行
来检查字典中是否存在键,而是通过执行price==inventory\u price.keys()
或if price in inventory\u price
来使用if price in inventory\u price.keys()
中的
操作符检查成员身份,此时您正在将字符串与列表进行比较,这是不对的
- 通过执行
,您无法获取键的值,而是通过执行inventory\u price.values()
或inventory\u price.get(price)
,此时您正在打印所有可用值inventory\u price[price]
- 您可以通过
print语句将关键字转换为列表,从而改进list(inventory\u price.keys())
print语句的格式What is the price..
inventory_price = {'hair_jel' : 1200, 'hair_oil' : 500, 'facial_kit' :
2900, 'moisturising_cream' : 200, 'comb' : 150 , 'scissors' : 300,
'hair_color' : 450}
price = input("What is the price of {} :".format(list(inventory_price.keys())))
#If key is present in dictionary
if price in inventory_price:
#Get the price
print(inventory_price[price])
或者,一种优化的方法是使用dict.get
,它返回键存在时的值,否则返回prdefined值,例如在这种情况下,未列出
inventory_price = {'hair_jel' : 1200, 'hair_oil' : 500, 'facial_kit' :
2900, 'moisturising_cream' : 200, 'comb' : 150 , 'scissors' : 300,
'hair_color' : 450}
price = input("What is the price of {} :".format(list(inventory_price.keys())))
#If key is present in dictionary, print the value, else print Not Listed
print(inventory_price.get(price, 'Not Listed'))
输出将是
What is the price of ['hair_jel', 'hair_oil', 'facial_kit', 'moisturising_cream', 'comb', 'scissors', 'hair_color'] :comb
150
What is the price of ['hair_jel', 'hair_oil', 'facial_kit', 'moisturising_cream', 'comb', 'scissors', 'hair_color'] :brush
Not Listed
这是因为如果
price
等于字典的键,则行price==inventory\u price.keys()
会进行比较
您必须验证字典中是否包含价格值
示例:如果存货中的价格\u价格
如果为真,请使用字典[键]
选择器选择值
inventory_price = {'hair_jel': 1200, 'hair_oil': 500, 'facial_kit': 2900,
'moisturising_cream': 200, 'comb': 150, 'scissors': 300,
'hair_color': 450}
price = input("What is the price of {} :".format(inventory_price.keys()))
if price in inventory_price:
print(inventory_price[price])
我想你需要这样的东西
inventory_price = {'hair_jel': 1200, 'hair_oil': 500, 'facial_kit':
2900, 'moisturising_cream': 200, 'comb': 150, 'scissors': 300,
'hair_color': 450}
price_key = input("What is the price of {}:".format(inventory_price.keys()))
default_price = "Price for '{}' Not Found".format(price_key)
price = inventory_price.get(price_key, default_price)
print(price)
如果希望所有值项都使用
.values()
获取所有值,如果希望所有元素都采用(键,值)
格式,则使用.items()
方法检查键并使用该键查找值@daniyalshabaz,您缺少正确的方法