Python中的购物总费用

时间:2019-04-07 15:10:33

标签: python

我正在尝试使用Python开发一个简单的程序,该程序会问我购物清单中的每个项目要花费多少。

我希望程序重复执行,每次问我要增加总价的下一个价格。

列表不能超过十个项目,当输入第十个项目时,我希望程序停止并显示所有项目的总成本,此外,我希望这样做,因此如果输入“ -1”,程序将执行就像第十项一样。

我有点想知道如何不将“ -1”添加到总数中,以及哪些变量是必需的,以便存储运行成本并计算已输入的项目。也不确定我对IF语句的语法。

enter = int(input("How much is the item: "))
enter2 = int(input("Next item: "))
enter3 = int(input("Next item: "))
enter4 = int(input("Next item: "))
enter5 = int(input("Next item: "))
enter6 = int(input("Next item: "))
enter7 = int(input("Next item: "))
enter8 = int(input("Next item: "))
enter9 = int(input("Next item: "))
enter10 = int(input("Next item: "))
total = enter + enter1 + enter2 + enter3 + enter4 + enter5 + enter6 + enter7 + enter8 + enter9 + enter10
print("Total price is", total)

if input == -1 or > 10:
   print("Total price is", total)

5 个答案:

答案 0 :(得分:1)

在继续下一个输入之前先进行输入验证

i=0
enter = []
while i < 10:
   item = int(input("Next item: "))
   if item == -1:
       break
   enter.append(item)
print("Total is: " + str(sum(enter)))

答案 1 :(得分:0)

我希望这会有所帮助:

enter = int(input("How much is the item: "))
if enter != -1:enter2 = int(input("Next item: "))
if enter2 != -1:enter3 = int(input("Next item: "))
if enter3 != -1:enter4 = int(input("Next item: "))
if enter4 != -1:enter5 = int(input("Next item: "))
if enter5 != -1:enter6 = int(input("Next item: "))
if enter6 != -1:enter7 = int(input("Next item: "))
if enter7 != -1:enter8 = int(input("Next item: "))
if enter8 != -1:enter9 = int(input("Next item: "))
if enter9 != -1:enter10 = int(input("Next item: "))
total = enter + enter1 + enter2 + enter3 + enter4 + enter5 + enter6 + enter7 + enter8 + enter9 + enter10
print("Total price is", total)

答案 2 :(得分:0)

if语句存在语法错误。

if input == -1 or > 10:

应该是这样的:

if input == -1 or input > 10:

答案 3 :(得分:0)

使用循环https://realpython.com/python-for-loop/

total = 0
for i in range(10)
    itemPrice = int(input("Next item: "))
    if (itemPirce = -1):
        break
    total = total + itemPrice
print("Total price is", total)

对于范围在(10)中的i-迭代10次,如下所示: i = 0,i = 1,..,..,i = 8,i = 9

总共10次。

如果要退出循环,请使用“ break”命令。 只有在价格为-1时,才会激活此处休息。

答案 4 :(得分:0)

我不确定如何最好地解决您的问题-具体来说,这是python 2还是3?我猜了些,并试图支持两者。我还添加了一些味道。希望你不介意!

from __future__ import print_function

from random import randint
from time import sleep

INVALID_INPUT = -2
GIMME_A_SUBTOTAL = -1
ALL_DONE = -3

def safe_int(v):
    try:
        return int(v)
    except ValueError:
        return INVALID_INPUT


def prompt_for_number(label):
    return safe_int(input("{0}\n> ".format(label)))


def make_label():
    yield "How much is the item?"
    while True:
        yield "How much is the next item?"


def cart_status(cart, final=False):
    if len(cart) == 0:
        return "Your cart is empty"
    return "Your {0}total is {1}".format(("" if final else "sub"), sum(cart))


def simulate_calculation():
    max_itr = randint(4, 9)
    for i in range(max_itr):
        print("Calculating Items{0}{1}\r".format("."*(i%4), "   "), end="", flush=True)
        sleep(.25)
    print()


def barcode_scanner():
    stop_early = []
    cart_items = []
    queries = {
        INVALID_INPUT: lambda : "Invalid input. Please supply a Natural Number.",
        GIMME_A_SUBTOTAL: lambda : cart_status(cart_items),
        ALL_DONE: lambda : stop_early.append(True),
    }
    label_maker = make_label()

    while len(cart_items) < 10 and len(stop_early) == 0:
        v = prompt_for_number(next(label_maker))
        msg = queries.get(v, lambda: "Beep".format(cart_items.append(v)))()
        print(msg)

    simulate_calculation()
    print(cart_status(cart_items, True))
    print("Thank you for your patronage!")


if __name__ == '__main__':
    try:
        barcode_scanner()
    except KeyboardInterrupt:
        print("\nCancelling the transcation")

快速注释:如果您使用的是python 3,则可以选择使用格式化的字符串,并替换以下行: "{0}\n>".format(label) 有了这个: f"{label}\n>"