我在比萨店problem上的代码有问题,
下面是评分器将通过该功能的示例输入之一
>>>cost_calculator([], ["ham", "anchovy"], drinks=["tub", "tub"], coupon=0.1)
35.61
[]代表比萨饼,第二个[“ ham”,“ anchovy”]是带有馅料的比萨饼。
让我感到困惑的主要事情之一是我如何将[]表示为披萨,价值13美元。我有一个字典占位符,但是不起作用,因为[]不可散列。我面临的另一个问题是如何处理顶部中包含的浇头。我的函数签名也不正确,我过去曾多次遇到该问题,如果有人可以指出我的资源来解释它们,我将非常感激。但是,我对字典的使用非常有信心。
def cost_calculator(x,wings,drinks,coupon):
total_cost = 0
x = {[]:13}
Price_of_drinks = {"small": 2.00, "medum":3.00,"large":3.50,"tub":3.75}
Price_of_wings = {10:5.00,20:9.00,40:17.50,100:48.00}
Price_of_toppings = {"pepperoni":1.00,"mushroom":0.50,"olive":0.50,"anchovy":2.00,"ham":1.50}
if x in iter
total_cost += x[[]]
if "pepperoni" in x
total_cost += 1.00
if "mushroom" in x
total_cost += 0.50
if "olive" in x
total_cost += 0.50
if "anchovy" in x
total_cost += 2.00
if "ham" in x
total_cost += 1.50
if wings in iter
total_cost += Price_of_wings['wings']
if drinks in iter:
total_cost += Price_of_drinks['drinks']
if wings in iter:
total_cost += Price_of_wings['wings']
if coupon in iter
total_cost= total_cost - (total_cost*coupon)
total_cost *= 1.0625
round(total_cost,2)
return total_cost
我只是在寻找正确方向的指针,如果您觉得我的问题不对,请确保进行编辑。我了解我的知识非常基础,因此指针很重要。
答案 0 :(得分:0)
我没有足够的声誉来发表评论,所以我帮不上您的披萨。 但是,对于饮料和优惠券,您应该将功能声明从
更改为mRecyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return true;
}
});
到
def cost_calculator(x,wings,drinks,coupon):
每次引用饮料或优惠券时,请分别使用kwargs ['drinks']和kwargs ['coupon']。调用该函数时,您可以输入(drinks = foo,coupon = bar)。
答案 1 :(得分:0)
def cost_calculator(x, wings, drinks, coupon):
total_cost = 0
Price_of_drinks = {"small": 2.00, "medum":3.00,"large":3.50,"tub":3.75}
Price_of_wings = {10:5.00,20:9.00,40:17.50,100:48.00}
Price_of_toppings = {"pepperoni":1.00,"mushroom":0.50,"olive":0.50,"anchovy":2.00,"ham":1.50}
Price_of_pizza = {"mypizza":13}
遍历列表x
,我将x
中的每个元素称为pizza
。
for pizza in x:
total_cost += Price_of_pizza[pizza] #Single pizza alone
print("Cost after adding single pizza with no topping: ", total_cost) #13
total_cost += Price_of_pizza[pizza] #Going to add pizza price and then toppings in the next loop you see
print("Cost after adding another single pizza with topping whose price is going to be added later ", total_cost) #13 + 13
for topping in wings:
total_cost += Price_of_toppings[topping]
print("Cost after adding single pizza with no topping + single pizza with two toppings requested ", total_cost) #13 + 13 + 1 + 2
for drinksize in drinks:
total_cost += Price_of_drinks[drinksize]
print("Cost after adding drinks ", total_cost) #13 + 13 + 1 + 2 + 3.75 + 3.75 = 37.0
total_cost = total_cost - total_cost*coupon #37.0 - 37.0*0.1 = 33.3
print("Cost after discount ", total_cost)
round(total_cost,2)
return total_cost
cost_calculator(['mypizza'], ["ham", "anchovy"], drinks=["tub", "tub"], coupon=0.1)