pizza shop函数中的循环问题

rsaldnfx  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(312)

我目前正试图解决一个有关比萨饼店的编码问题。它意味着接受多个参数(从1到多个参数),并将每个参数的固定成本相加以返回一定的成本。我的问题是,当前的代码有什么问题,它不能循环遍历数据。我制作了一个函数、一个字典和一个for循环来遍历数据。所有这些都是用python编写的。

def cost_calculator(*x, wings, drinks, coupon):

total_cost = 0  #variable that holds final cost of order
print(total_cost)

pizza_to_price = {"mypizza":13}
drinks_to_price = {"small": 2.00, "medium": 3.00,"large": 3.50,"tub": 3.75}
wings_to_price = {10:5.00, 20:9.00, 40:17.50, 100:48.00}
toppings_to_price = {"pepperoni":1.00, "mushroom":0.50, "olive":0.50, "anchovy":2.00,"ham":1.50}

for pizza in x:
    total_cost += 13.00
for topping in wings:
    total_cost += wings_to_price[topping]
for size in drinks:
    total_cost += drinks_to_price[size]
for discount in coupon:
    total_cost = total_cost - (total_cost * coupon)
total_cost *= 1.0625

round(total_cost,2)
return total_cost

回复:我得到的错误是,如果我取消注解所有内容,就会返回一个nonetype值。

guicsvcw

guicsvcw1#

我不知道这是不是打字错误,但你忘了缩进:

def cost_calculator(*x, wings, drinks, coupon):

    total_cost = 0.0  #variable that holds final cost of order

    pizza_to_price = {"mypizza":13}
    drinks_to_price = {"small": 2.00, "medium": 3.00,"large": 3.50,"tub": 3.75}
    wings_to_price = {10:5.00, 20:9.00, 40:17.50, 100:48.00}
    toppings_to_price = {"pepperoni":1.00, "mushroom":0.50, "olive":0.50, "anchovy":2.00,"ham":1.50}

    for pizza in x:
        total_cost += 13.00
    for topping in wings:
        total_cost += wings_to_price[topping]
    for size in drinks:
        total_cost += drinks_to_price[size]
    for discount in coupon:
        total_cost = total_cost - (total_cost * float(discount))
    total_cost *= 1.0625

    round(total_cost,2)
    return total_cost

pizzas = 3
wings = [10, 20]
drinks = []
discounts = [1.5, 2]
print(cost_calculator(pizzas, wings = wings, drinks = drinks, coupon = discounts))

结果: 14.34375 python函数:https://www.w3schools.com/python/python_functions.asp
pythonMap:https://www.geeksforgeeks.org/python-map-function/

wvt8vs2t

wvt8vs2t2#

我不知道您是否需要,但我对@silverfox代码做了一些编辑:

def cost_calculator(x, wings, drinks, top, coupon):
    if x==0:
        return 0
    total_cost = 0.0  #variable that holds final cost of order

    pizza_to_price = {"mypizza":13}
    drinks_to_price = {"small": 2.00, "medium": 3.00,"large": 3.50,"tub": 3.75}
    wings_to_price = {10:5.00, 20:9.00, 40:17.50, 100:48.00}
    toppings_to_price = {"pepperoni":1.00, "mushroom":0.50, "olive":0.50, "anchovy":2.00,"ham":1.50}

    total_cost += 13.00*x
    for topping in wings:
        total_cost += wings_to_price[topping]
    for size in drinks:
        total_cost += drinks_to_price[size]
    for tops in top:
        total_cost += toppings_to_price[tops]
    for discount in coupon:
        total_cost = total_cost - (total_cost * float(discount))
    total_cost *= 1.0625

    return round(total_cost,2)

pizzas = 3
wings = [10, 20]
drinks = ["large"]
discounts = [1.5, 2]
topps=["pepperoni"]
print(cost_calculator(pizzas, wings = wings, drinks = drinks,top=topps, coupon = discounts))

Out : 30.55

相关问题