【发布时间】:2020-07-23 07:13:24
【问题描述】:
我在将一个真正多余的字典转换为一个函数(def)时遇到了麻烦
可以正常工作的原始代码是:
Pen = (9,'always','monday')
Paper = (1,'always','tues')
PriceDic = {'Pen': Pen[0],
'Paper': Paper[0]}
while True:
name = input("name of the product?")
print(PriceDic.get(name),'dollar')
break
打印为...
>>>name of the product?Pen
>>>9 dollar
但是问题是
- 我不仅有笔和纸,还可能还有 100-200 个元组要写
- 并且每个元组都需要包含多个信息......所以这个程序的最终目标是能够从元组索引中获取各种信息并打印它们。
所以
我想也许我可以运行并编写这段代码......
def FindPriceFunction(x):
Pen = (9,'always','monday')
Paper = (1,'always','tuesday')
FindPriceDic = { x : x[0]}
print(FindPriceDic.get(x),'dollar')
while True:
name = input("name of the product?")
FindPriceFunction(name)
break
这给了我...
>>>name of the product?Pen
>>>P dollar
请帮助我
【问题讨论】:
标签: python function dictionary tuples