2017-09-22 85 views
-1

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

你可以通過列表作爲參數傳遞給你的函數 – Fabricator

+0

你能提供的如何做到這一點的例子,因爲我不是很確定? @Fabricator –

回答

0

如果你把你的訂單選擇成2D列表,它可以更容易索引的東西。

def process_choice(quantity,stock,price,total): 
    if quantity > stock: 
     print("There is not enough stock!") 
     pass 
    else: 
     total += price*quantity 
     stock -= quantity 
    return total, stock   


total = 0  
A = [["Big Mac", float(2.50), 50],["Large Fries", float(0.50), 200],["Vegetarian Burger", float(1.00), 20]] 

print(A[0]) 
print(A[1]) 
print(A[2]) 

while True: 
    choice = (input("What would you like?")).upper() 
    quantity = float(input("How many would you like?")) 

    choices = [A[0][0].upper(),A[1][0].upper(),A[2][0].upper()] 
    idx = choices.index(choice) 

    total, A[idx][2] = process_choice(quantity,A[idx][2],A[idx][1],total) 

    more_items = (input("Do you want to order more items?")).lower() 
    if more_items == "yes": 
     pass 
    else: 
     break 

print("Thank you for ordering!") 
print("Your total cost is:", total) 
1

您必須製作一個具有通用功能的功能,併爲每種情況傳遞不同的參數。

功能:

def processOrder(quantity, stock, total): 
    if quantity > stock[2]: 
     print("There is not enough stock!") 
     pass 
    else: 
     total += stock[1] * quantity 
     stock[2] -= quantity 

整個程序:

def processOrder(quantity, stock, total): 
    if quantity > stock[2]: 
     print("There is not enough stock!") 
     pass 
    else: 
     total += stock[1] * quantity 
     stock[2] -= quantity 

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

     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) 
if choice == "BIG MAC": 
     processOrder(quantity, A1, total) 

    elif choice == "LARGE FRIES": 
     processOrder(quantity, B1, total) 

    elif choice == "VEGETARIAN BURGER": 
     processOrder(quantity, C1, total) 
+1

這將始終使用'A1',也爲大薯條和漢堡素食。 – mkrieger1

+0

我只是固定它現在是傳遞正確的列表中的所有他們的 – DobromirM

2

轉換的項目清單到字典將是有益的,並會減少其如果說出所有的 - ELIF -else陳述

total = 0 

item_dict = { 
"BIG MAC": [float(2.50), 50], 
"LARGE FRIES": [float(0.50), 200], 
"VEGETARIAN BURGER": [float(1.00), 20]} 

def order_adder(menu_item, quantity, total): 
    try: 
     if item_dict[menu_item][1] - quantity < 0: 
      print("There is not enough stock!") 
     else: 
      item_dict[menu_item][1] = item_dict[menu_item][1] - quantity 
      total += item_dict[menu_item][0]*quantity 
    except(KeyError): 
     print("We don't sell that item") 
    return total 

while True: 
    choice = (input("What would you like?")).upper() 
    quantity = float(input("How many would you like?")) 

    total = order_adder(choice, quantity, total) 

    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) 
+0

你應該使用'item_dict.get()'訪問鍵,它是不安全的信任用戶的輸入,他一定會嘗試着問一些威士忌一次 – PRMoureu

+0

@PRMoureu這只是用'TypeError'交換'KeyError'。 –

+0

@AdamSmith是啊,除非你給一些備用值,以防止這一點,或測試'item_dict.get(選擇)'之前調用'order_adder' – PRMoureu

0

扭捏DobromirM的回答是:

total = 0 
def processOrder(quantity, stock): 
    global total 
    if quantity > stock[2]: 
     print(quantity, stock[1]) 
     print("There is not enough stock!") 
     pass 
    else: 
     total += stock[1] * quantity 
     stock[2] -= quantity 

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": 
     processOrder(quantity, A1) 
    elif choice == "LARGE FRIES": 
     processOrder(quantity, B1) 
    elif choice == "VEGETARIAN BURGER": 
     processOrder(quantity, C1) 
    more_items = (input("Do you want to order more items?")).lower() 
    if more_items == "yes": 
     pass 
    else: 
     break 

print("Thank you for ordering!\nYour total cost is:", total) 
相關問題