超市计费系统

时间:2018-10-04 11:54:14

标签: python billing price

我是Python的新手,正在从事Supermarket Billing系统。 到目前为止,我知道了:

# 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]

那么,就像芒果的价格是23.0,面粉是43.02,并据此进行计算,这会自动得出吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

制定价格数组。

声明价格。 因此,您的输出将是:输入水果名称。
输入水果名称后,它将立即获取其地址并将其乘以固定价格。

声明代码

prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

声明价格,然后通过函数调用它。