Pradeep
Pradeep

Reputation: 13

why I am getting the wrong output?

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)

The code should calculate the cost of the meal under given conditions ( line 1 to 5 ). These conditions can change. The output of the above code should be 5.0 but I am getting an output equals to 5.5. The scenario is the last else statement of the code where base_price should be 5+0.00 = 5.00 as the price of burrito is 5.0. So, how I am getting 5.5?

Upvotes: 0

Views: 621

Answers (1)

Kevin Fang
Kevin Fang

Reputation: 2012

You should replace

if meat == "steak" or "pork":

with

if meat == "steak" or meat == "pork":

Explanation:

meat == "steak" or "pork" will be executed sequentially (== has higher priority than or), so meat=="steak" is False, and the expression will be False or "Pork" which is "pork" which is True.

>>> meat = 'chicken'
>>> meat == 'steak' or 'pork'
'pork'
>>> meat == 'steak' or meat == 'pork'
False

Upvotes: 1

Related Questions