【发布时间】:2016-10-27 14:33:29
【问题描述】:
我正在尝试通过预制字典中的另一部分代码来对用户给定的号码进行复数。这是我的示例代码
randomdict = {'22' : {'name' : 'sponge', 'price' : 1}}
choice = input("Enter a number: ")
amount = input("How much of that item?: ")
if choice in randomdict:
price = amount * (randomdict['price'])
print("Price is "+str(price))
如您所见,我正在尝试获取用户输入的金额乘以保存在字典 randomdict 中的商品价格。我该怎么做呢?试了好几次都搞不定
【问题讨论】:
-
选择和金额是字符串。您需要先将它们解析为数字。
-
如果我这样做,那么
price = amount * (randomdict['price'])行是否正确? -
一旦等式中的所有内容都是数字,是的,它应该可以工作。
标签: python dictionary input