0
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?

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
Pradeep
  • 15
  • 1
  • 1
  • 6
  • _Why_ should you get "5.0"? If we don't know what the code should do, and you have a math error, it's impossible for us to answer – roganjosh Nov 14 '18 at 00:18
  • The code should calculate the cost of the meal under given conditon( line 1 to 5 ) – Pradeep Nov 14 '18 at 00:20
  • Dupe: https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value – roganjosh Nov 14 '18 at 00:30
  • @roganjosh well I am not allowed to use lists or set. I must execute this program completely with if..elif...else statement – Pradeep Nov 14 '18 at 00:33

1 Answers1

1

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
Kevin Fang
  • 1,966
  • 2
  • 16
  • 31
  • You edited your explanation out :P Why does `meat == 'steak' or 'pork'` print `pork`? – roganjosh Nov 14 '18 at 00:26
  • This is a bit of a rabbit hole of an answer, because the first question I would ask is why I get a bool for one condition and a string for another. It's best to close such questions as dupes unless you have a comprehensive answer – roganjosh Nov 14 '18 at 00:33
  • @roganjosh more than happy to do that, not enough reputation though – Kevin Fang Nov 14 '18 at 00:40
  • It's probably just personal preference, but I find `if meat in ('steak', 'pork')` more concise and readable. – kungphu Nov 14 '18 at 01:24