【问题标题】:Python programming help?Python 编程有帮助吗?
【发布时间】:2011-11-18 08:56:24
【问题描述】:

我一直在尝试制作类似代码的收银机,用户可以在其中输入产品名称、价格和数量。我有四种产品。在完成所有这些添加后,我需要获得 5% 的 GST,然后打印出包括 GST 在内的总金额。这是我到目前为止所能提供的,而且我是 python 新手,这就是为什么我不知道很多错误和其他关键字的原因。我得到和 ut 一切,但是当我相乘时,它说它不能将字符串和整数相乘。我尝试更改变量名称并做了所有其他的事情,但它不会给出总数。

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)

【问题讨论】:

  • 请告诉我热得到总数,我该如何解决谢谢
  • 如果我只是告诉你如何解决它,我将不得不为你编写一个全新的程序,而你将一无所获。您编写这样一个程序的原因有两个:作为家庭作业或出于您自己的学习目的。在这两种情况下,我都不想给你一个完整的解决方案。
  • @Yousuf 你真的应该听从 Björn Pollex 的建议,并可能阅读一个很好的教程和其他介绍性材料。两个很好的可能性是 python 文档和在线书籍“Dive into Python”。
  • 我现在肯定会先阅读文档
  • “先阅读文档” -> 这是一个绝妙的主意。这样,您将不会学会解决给定的简单任务,但您将成为真正的程序员!在网上找到的盲目复制粘贴sn-ps和真正有想法、解决问题的区别……

标签: python


【解决方案1】:

可以简化很多。

询问产品的代码可以简化为:

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

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

suggested_gst = .05 * total_sum

您还可以打印价格和数量的产品列表:

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

【讨论】:

  • 谢谢我不明白我的代码很长,因为我是新手。看到我不知道很多东西,所以我不打算使用它,那是你想学习的,只是试着在这个中得到总数,如果我不能让 gst 工作,我会问一个问题,但非常感谢真的很欣赏它
  • 对不起,但是当我把它放进去时,我得到了:Traceback(最后一次调用):文件“/home/yousuf/NetBeansProjects/pythonpretest/src/pythonpretest.py”,第 18 行, in total_sum = sum([i['price']*i['quantity'] for i in a]) NameError: name 'a' is not defined
  • @YousufSiddiqui:我已经粘贴了具有a 变量而不是products 的代码,忘记更改它。我现在已经更正了我的答案,因此它更具可读性并且应该适合您。
  • 这是我使用您的简化版本时得到的:文件“/home/yousuf/NetBeansProjects/pythonpretest/src/pythonpretest.py”,第 14 行返回产品 SyntaxError:'return' 外部函数
  • @YousufSiddiqui:已更正 :) 我的 return products 缩进不正确(将代码复制到 StackOverflow 时出现错误)。
【解决方案2】:

我在这里看到了很多奇怪的东西。我会推荐以下方法:

  1. 编写您的程序,以便它可以处理单个产品。
  2. 然后添加第二个产品。
  3. 现在考虑一个循环。

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

【讨论】:

  • 我是新手,所以我不知道如何制作循环,但为什么价格和数量呢?在我得到总数后,我想先把这两个和下一个分开,依此类推,然后把它们全部放在一起
【解决方案3】:

什么是 sum_total? 你在写

sum_total = something, something else, something else again

(注意“,”)

写出来不是更好吗

sum_total = something + something else + something else again

? (注意“+”号)

您的第一行是一个元组(在 python 文档中搜索它!)而不是一个数字。

【讨论】:

    【解决方案4】:

    sum_total 是 tuple。你不应该把它们加在一起吗?

    【讨论】:

    • 我不确定你能否告诉我在哪里以及如何做到这一点。我在谷歌上搜索过,但我不明白我是新手。
    • @YousufSiddiqui 检查 Francesco 的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-12-20
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多