我希望通过添加函数来提高代码的效率。 if语句中的任务实际上是相同的 - 只有变量才会发生变化。但是,我不知道如何根据用户对该功能的选择来更改列表。有谁知道这样做的方法?
fs.createReadStream(`${__dirname}/index.html`, {encoding: 'utf8'})
答案 0 :(得分:2)
将您的项目列表转换为dict会有所帮助,并且可以减少必须声明所有if-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)
答案 1 :(得分: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)
答案 2 :(得分: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)
答案 3 :(得分: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)