我在收据时遇到问题。它应该能够接受多种价格(只有1和无数(如果你想要那么高))并且能够对它们进行小计,对它征税并获得总价。好吧,我无法弄清楚如何添加多个数字。这就是我不断得到的 (我不得不删除(<,>)并将这些(",")展示给它的内容。
when you are finished enter -1
enter price of item: 5.22
enter price of item: 6.35
enter price of item: -1
your subtotal is: "function subtotal at 0x0314C9C0"
your tax is: "function tax at 0x0314C978"
your total is: "function total at 0x0314CA08"
you have bought 2 items
Thank You for Shopping at Qmart
这是我到目前为止的编码
print("Qmart Receipt".center(78, "-"))
print("when you are finished enter -1")
def price(subtotal, items, price):
counter = 0
while counter <= items:
subtotal = (items + items)
def subtotal():
subtotal = sum(input)
def tax():
tax = subtotal * .065
def total():
total = subtotal + tax
counter = 0
price = input("enter price of item: ")
while (True):
price = float(input("enter price of item: "))
counter += 1
if (price == -1):
print("your subtotal is: ", subtotal)
print("your tax is: ", tax)
print("your total is: ", total)
break;
print("you have bought", counter, "items")
print("Thank You for Shopping at Qmart")
答案 0 :(得分:0)
首先:使用return
语句返回结果 - 为像你这样的本地函数赋值,在Python中没有任何意义。
所以而不是
def subtotal():
subtotal = sum(input)
def tax():
tax = subtotal * .065
def total():
total = subtotal + tax
使用:
def subtotal():
return sum(input)
def tax():
return subtotal() * .065
def total():
return subtotal() + tax()
其次,我已经在上面的代码段中完成了调用一个没有参数的函数,将()
放在其名称之后 - 只需命名一个函数不会调用它。这也适用于最后的print
- 您需要在您呼叫的每个功能名称之后添加()
。
subtotal
更改为:
def subtotal():
return sum(prices)
(不使用input
,这是内置函数的名称,用于命名价格列表!);并将循环更改为
prices = []
while True:
price = float(input("enter price of item: "))
if price != -1: prices.append(price)
我还删除了无用变量counter
- 在倒数第二个len(prices)
中使用print
。
将所有这些更改汇总到他们的代码中OP应该有一个工作程序。不,OP,在你问之前:我不要做&#34;组合&#34;代表您 - 请通过自己动手显示某些工作神经元的证据: - )