item = "burrito"
meat = "chicken"
queso = False
guacamole = False
double_meat = False
if item == "quesadilla":
base_price = 4.0
elif item == "burrito":
base_price = 5.0
else:
base_price = 4.5
if meat == "steak" or "pork":
if double_meat:
if guacamole and queso and not item == "nachos":
base_price += 1.00 + 1.50 + 0.50 + 1.00
elif guacamole:
base_price += 0.50 + 1.50 + 1.00
else:
base_price += 0.50 + 1.50
else:
if guacamole and queso and not item == "nachos":
base_price += 1.00 + 0.50 + 1.00
elif guacamole:
base_price += 0.50 + 1.00
else:
base_price += 0.50
else:
if double_meat:
if guacamole and queso and not item == "nachos":
base_price += 1.00 + 1.00 + 1.00
elif guacamole:
base_price += 1.00 + 1.00
else:
base_price += 1.00
else:
if guacamole and queso and not item == "nachos":
base_price += 1.00 + 1.00
elif guacamole:
base_price += 1.00
else:
base_price += 0.00
print(base_price)
代码应计算给定条件(第1至5行)下的餐食费用。这些条件可以改变。上面代码的输出应为5.0
,但我得到的输出等于5.5
。
该方案是代码的最后else
条语句,其中base_price
应该是5+0.00 = 5.00
,因为墨西哥卷饼的价格是5.0
。那么,我如何获得5.5
?
答案 0 :(得分:1)
您应该替换
if meat == "steak" or "pork":
与
if meat == "steak" or meat == "pork":
说明:
meat == "steak" or "pork"
将顺序执行(==
的优先级高于or
),因此meat=="steak"
为False,表达式为False or "Pork"
,即"pork"
是对的。
>>> meat = 'chicken'
>>> meat == 'steak' or 'pork'
'pork'
>>> meat == 'steak' or meat == 'pork'
False