我一直在尝试制作一个类似代码的收银机,用户在其中键入他们的产品名称,价格和数量。我有四种产品。完成所有这些添加后,我需要获得5%的商品及服务税,然后打印出总金额,包括商品及服务税。到目前为止,这就是我能想到的,而且我是python的新手,这就是为什么我不知道很多错误和其他关键字的原因。我把所有东西都写成“和”,但是当我做乘法时,它说它不能把字符串和整数相乘。我尝试更改变量名,并做了所有其他事情,但它不会给出一个总数。
name1 =raw_input('What Item do you have: ')
price1 = float(input('What is the price of your item: '))
quantity1 = float(input('How many are you buying: '))
name2 = raw_input('What Item do you have: ')
price2 = float(input('What is the price of your item: '))
quantity2 = float(input('How many are you buying: '))
name3 = raw_input('What Item do you have: ')
price3 = float(input('What is the price of your item: '))
quantity3 = float(input('How many are you buying: '))
name4= raw_input('What Item do you have: ')
price4 = float(input('What is the price of your item: '))
quantity4 = float(input('How many are you buying: '))
sum_total= (price1 * quantity1), (price2 * quantity2), (price3 * quantity3), (price4 * quantity4),
print(' %.2f ' % quantity1+quantity2+quantity3,' X ', name1+name2+name3,' @ %.2f ' %
price1+ price2+price3,' = %.2f ' % total)
divv = sum_total / 100
percent = divv * 0.05
gst = sum_total + percent
print('The suggested gst is %.2f '% percent )
print('That will be a total of: %.2f '% gst)发布于 2011-11-18 17:09:04
它可以简化很多。
您查询产品的代码可以简化为:
def ask_for_products(how_many):
products = []
for i in xrange(how_many):
product = {
'name': raw_input('What Item do you have: '),
'price': float(input('What is the price of your item: ')),
'quantity': float(input('How many are you buying: '))
}
products.append(product)
return products这将使您的代码更加灵活和模块化。
总和可以这样计算(假设products包含上述函数的结果):
total_sum = sum([i['price']*i['quantity'] for i in products])如果我没理解错的话,建议的商品及服务税是:
suggested_gst = .05 * total_sum您还可以打印包含价格和数量的产品列表
for p in products:
print '%.2f X %.2f %s' % (p['quantity'], p['price'], p['name'])发布于 2011-11-18 17:03:56
我在这里看到了很多奇怪的东西。我建议使用以下方法:
如果您在特定步骤中遇到问题,请在此处提出问题。
发布于 2011-11-18 17:04:26
什么是sum_total?你在写
sum_total = something, something else, something else again(请注意",")
写一篇文章不是更好吗
sum_total = something + something else + something else again?(注意"+“号)
您的第一行是一个元组(在python文档中搜索它!)而不是一个数字。
https://stackoverflow.com/questions/8179902
复制相似问题