首页 文章

Python编程有帮助吗?

提问于
浏览
0

我一直试图像代码一样制作收银机,用户在产品名称中输入价格和数量 . 我有四个产品 . 完成所有这些添加之后,我需要获得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)

4 回答

  • 1

    它可以简化很多 .

    您的 asking about products 代码可以简化为:

    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
    

    这将使您的代码更加灵活和模块化 .

    Total sum 可以像这样计算(假设 products 包含上述函数的结果):

    total_sum = sum([i['price']*i['quantity'] for i in products])
    

    Suggested GST ,如果我理解正确的话,是:

    suggested_gst = .05 * total_sum
    

    你也可以打印 list of products with prices and quantities

    for p in products:
        print '%.2f X %.2f %s' % (p['quantity'], p['price'], p['name'])
    
  • 0

    什么是sum_total?你在写

    sum_total = something, something else, something else again
    

    (注意“,”)

    写作不是更好

    sum_total = something + something else + something else again
    

    ? (注意“”标志)

    你的第一行是一个元组(在python docs上搜索它!)而不是数字 .

  • 1

    我在这看到许多奇怪的事情 . 我建议采用以下方法:

    • 编写程序以便它可以处理单个产品 .

    • 然后添加第二个产品 .

    • 现在想一个循环 .

    如果您在执行特定步骤时遇到问题,请在此处提出问题 .

  • 1

    sum_totaltuple . 你不应该把它们加在一起吗?

相关问题