【发布时间】:2018-10-04 11:54:13
【问题描述】:
我是 Python 的新手,正在研究超市计费系统。 到目前为止,我得到了这个:
# Supermarket Billing
a = 0
b = 0
c = 0
while True:
item = input('Item: ')
amount = input('Amount: ')
a = int(amount)
price = input('Price: ')
b = int(price)
c += a * b
next_item = input('Is there another item? ')
if next_item != 'Yes':
print('Please choose between Yes or No')
if next_item == 'No':
break
print('Total: ', c)
一切正常。但我想省略价格。就像用户输入“Mango”一样,它应该会自动从代码中获取价格,并将金额与价格相乘以获得总和。
我之前尝试过:
if item == 'Mango'
final_price_mango = a * 23.0 #Let's say price is 23.0
因此,对于每件商品,我都必须在不同的代码块中给出商品的价格,从而使代码变得冗长。没有通用的方法吗? 比如说,把价格列在一个列表中
[23.0, 43.02, 5.6, 100.75]
所以自动会像 Mango 的价格是 23.0,Flour 是 43.02 并相应地计算?
提前致谢!
【问题讨论】:
-
你应该看看dictionaries和here
-
@roganjosh 我会尝试一次。