我想使代碼更高效,加入的功能。 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 –