如何在python中添加未知值

2024-06-07 00:35:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个收银机程序。我的目标是所有的产品都被添加,并找到最终的价格。如果不知道以前的价值观,我怎么能达到这个目标?下面是我的代码。提前感谢您的帮助,不胜感激

responses = {}

polling_active = True

print("Welcome to the cash register. Insert your product name and price to be able to calculate your final total")


total = 0

while polling_active:
    product = input("\nProduct Name: ")
    total += float(input("Price: "))

    responses[product] = total

    repeat = input("Is this your final checkout? (Yes/No)")
    if repeat == 'no':
        polling_active = True
    elif repeat == 'No':
        polling_active = True
    elif repeat == 'Yes':
        polling_active = False
    elif repeat == 'yes':
        polling_active = False
    else:
        print("That operation is invalid")

print("\n---Final Checkout---")
for product, price in responses.items():
    print(product + " is $" + str(total))

print("\n---Total Price---")
print("Store Price is: ")
print("$" + str(total))

print("\n---Tax Price---")
print("Your price with tax is: ")

total = total * 1.13

print("$" + "{0:.2f}".format(float(total)))

print("\nThank you for shopping with us! Have a great day!")

我理解与我目前的代码,总数将不允许我添加任何产品,但这只是目前的一个位置持有人


Tags: totrueinputyourisresponsesproductprice
1条回答
网友
1楼 · 发布于 2024-06-07 00:35:27

以下是您需要的代码:

responses = {}

polling_active = True

print("Welcome to the cash register. Insert your product name and price to be able to calculate your final total")

while polling_active:
    product = input("\nProduct Name: ")
    price = float(input("Price: "))

    responses[str(product)] = price

    repeat = raw_input("Is this your final checkout? (Yes/No)")
    if repeat.lower() == 'no':
        polling_active = True
    elif repeat.lower() == 'yes':
        polling_active = False
    else:
        print("That operation is invalid")

print("\n -Final Checkout -")
for product, price in responses.items():
    print(product + " is $" + str(price))

print("\n -Total Price -")
print("Store Price is: ")
total= sum(responses.values())
print("$" + str(total))

print("\n -Tax Price -")
print("Your price with tax is: ")

total = total * 1.13

print("$" + "{0:.2f}".format(float(total)))

print("\nThank you for shopping with us! Have a great day!")

输出:

整数: enter image description here

浮动: enter image description here

相关问题 更多 >