我想让我的程序显示订单总额/发票



我是这样做的:

food =["cheeseburger", "smallchips", "drink"]
prices =[2.50, 1.50, 1]
x=0
myorderfood=[]
myordercost=[]
print("Burgersn")
print("Menu:")
print("Cheeseburger. Cost - $2.50 each")
print("Small chips. Cost - $1.50 each")
print("Drink - Cola only. Cost - $1.00 eachn")

我想在用户完成订单后显示发票,显示总价。

这是我的一些代码只是饮料,相同类型的代码用于奶酪汉堡等:

while True:
try:
drinkselect = input("Please select which drink:n")
if  drinkselect == "cola":
quantitydrink = int(input("How many would you like?n"))
except ValueError:
print("Not valid")
continue
if drinkselect != "cola":
print("Not on our menu!n")
else:
print("good choicen")
break

你可以这样写:

from statistics import quantiles
from xml.etree.ElementPath import prepare_predicate

food =["cheeseburger", "smallchips", "drink"]
prices =[2.50, 1.50, 1]
x=0
price = 5
quantitydrink = 0
myorderfood=[]
myordercost=[]
print("Burgersn")
print("Menu:")
print("Cheeseburger. Cost - $2.50 each")
print("Small chips. Cost - $1.50 each")
print("Drink - Cola only. Cost - $1.00 eachn")
print("10% GST applies to total amount (including both food and delivery)")
print("Delivery is free for food orders that are $30 or more")
print("Delivery cost is an additional $5 for food orders below $30n")
while True:
try:
drinkselect = input("Please select which drink:n")
if  drinkselect == "cola":
quantitydrink += int(input("How many would you like?n"))
except ValueError:
print("Not valid")
continue
if drinkselect != "cola":
print("Not on our menu!n")
else:
print("good choicen")
price += 1 * quantitydrink
print(f"Your total cost is {price}!")
break

交货的基本价格是5,如果你选择可乐,它会将可乐的价格乘以你购买的数量加到价格变量中。

+=操作符是最好的。

变化:

price = 5 #added this on line 8 because the delivery cost is 5 so that is the base price
quantitydrink = 0 #added this on line 9 so you can add the amount to it later in the code
quantitydrink += int(input("How many would you like?n")) # changed this in line 26 to add the amount you are ordering to the initial value.
price += 1 * quantitydrink # added this in line 36 to calculate the price based on how many colas you are ordering and the base price
print(f"Your total cost is {price}!") #added this at line 37 to print the final price of the order

希望这对你有帮助!

包含所有修复的最终代码:

from statistics import quantiles
from xml.etree.ElementPath import prepare_predicate

finished = False
food =["cheeseburger", "smallchips", "drink", "delivery"]
prices =[2.50, 1.50, 1, 5]
x=0
price = 5
quantitydrink = 0
quantityburger = 0
quantitychips = 0
quantitydelivery = 0
myorderfood=[]
myordercost=[]
print("Burgersn")
print("Menu:")
print("Cheeseburger. Cost - $2.50 each")
print("Small chips. Cost - $1.50 each")
print("Drink - Cola only. Cost - $1.00 eachn")
print("10% GST applies to total amount (including both food and delivery)")
print("Delivery is free for food orders that are $30 or more")
print("Delivery cost is an additional $5 for food orders below $30n")
name = input("What is your name?n")
print("nHello " + name + "n")
while finished == False:
try:
burgerselect = input("Please select which burger:n")
if  burgerselect == "cheeseburger":
quantityburger += int(input("How many would you like?n"))            
except ValueError:
print("Not valid")
continue
if burgerselect != "cheeseburger":
print("Not on our menu!n")
else:
print("good choicen")
price += 2.5 * quantityburger
try:
chipselect = input("Please select what size chips:n")
if  chipselect == "small":
quantitychips += int(input("How many would you like?n"))
except ValueError:
print("Not valid")
continue
if chipselect != "small":
print("Not on our menu!n")
else:
print("good choicen")
price += 1.50 * quantitychips
try:
drinkselect = input("Please select which drink:n")
if  drinkselect == "cola":
quantitydrink += int(input("How many would you like?n"))
except ValueError:
print("Not valid")
continue
if drinkselect != "cola":
print("Not on our menu!n")
else:
print("good choicen")
price += 1 * quantitydrink
deliveryselect=input('Would you like delivery? n Yes: Deliveryn No: No delivery n n')
if deliveryselect=='Yes'or deliveryselect=='yes':
print('Thank Youn')
if price <= 30:
price += 5
elif price > 30:
price += 0
finished == True
break
elif deliveryselect =='No' or deliveryselect=='no':
print('Thank youn')
finished == True
break

print(f"Your total cost is {price}!")