我想使代码更高效,加入的功能。 if语句中的任务实际上是相同的 - 只有变量发生变化。但是,我不确定如何根据用户对该功能的选择来更改列表。有没有人知道一种方法来做到这一点?如何将不同的变量集成到一个函数中?
total = 0
A1 = ["Big Mac", float(2.50), 50]
B1 = ["Large Fries", float(0.50), 200]
C1 = ["Vegetarian Burger", float(1.00), 20]
print(A1[0:2])
print(B1[0:2])
print(C1[0:2])
while True:
choice = (input("What would you like?")).upper()
quantity = float(input("How many would you like?"))
if choice == "BIG MAC":
if quantity > A1[2]:
print("There is not enough stock!")
pass
else:
total += A1[1]*quantity
A1[2] -= quantity
elif choice == "LARGE FRIES":
if quantity > B1[2]:
print("There is not enough stock!")
pass
else:
total += B1[1]*quantity
B1[2] -= quantity
elif choice == "VEGETARIAN BURGER":
if quantity > C1[2]:
print("There is not enough stock!")
pass
else:
total += C1[1]*quantity
C1[2] -= quantity
more_items = (input("Do you want to order more items?")).lower()
if more_items == "yes":
pass
else:
break
print("Thank you for ordering!\n"
"Your total cost is:", total)
你可以通过列表作为参数传递给你的函数 – Fabricator
你能提供的如何做到这一点的例子,因为我不是很确定? @Fabricator –