【问题标题】:Supermarket Billing System超市计费系统
【发布时间】: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 并相应地计算?

提前致谢!

【问题讨论】:

标签: python billing price


【解决方案1】:

制作一个价格数组。

宣布价格。 所以你的输出将是:输入水果名称。
只要你输入水果名称,它就会把它的地址乘以固定价格。

声明代码

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 2013-02-21
    • 2011-04-23
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多