2017-09-22 110 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) 
相关问题