gpt4 book ai didi

python - 如何在python中添加未知值

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:33 25 4
gpt4 key购买 nike

我正在尝试创建一个收银机程序。我的目标是添加所有产品并找到最终价格。在不知道之前的值的情况下如何实现这个目标?下面是我的代码。在此先感谢您的帮助,非常感谢

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!")

我了解我当前的代码,total 不允许我添加任何产品,但目前它只是一个占位符

最佳答案

这是您需要的代码:

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

float : enter image description here

关于python - 如何在python中添加未知值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com