0

I am making a shopping program for school and everything was fine until I ran it before I submitted it. when I input 'n' for the 5th line, it runs the if statement anyway. I have checked over it a bunch of times but I can't seem to find anything wrong. I would appreciate it if you could help.

price1 = float(input('Cost of the first product($): '))
mass1 = float(input('Mass of the first product(g): '))
price2 = float(input('Cost of the second product($): '))
mass2 = float(input('Mass of the second product(g): '))
yes = input ("Do you want to add another product? ")

if yes == 'y' or 'yes':
  price3 = float(input('Cost of the third product($): '))
  mass3 = float(input('Mass of the third product(g): '))
  total1 = price1/mass1
  total2 = price2/mass2
  total3 = price3/mass3
  print('The unit price of Product One is', total1, '$/g.')
  print('The unit price of Product two is', total2, '$/g.')
  print('The unit price of Product three is', total3, '$/g.')
  if total1 < total2 and total1 < total3:
    print ('Product one is of better value.')
  elif total2 < total1 and total2 < total3:
    print ('Product two is of better value.')
  elif total3 < total2 and total3 < total1:
    print ('Product three is of better value.')
  else:
    print ('All products are of equal value.')
    
elif yes == 'n' or 'no':
  total1 = price1/mass1
  total2 = price2/mass2
  print('The unit price of Product One is', total1, '$/g.')
  print('The unit price of Product One is', total2, '$/g.')
  if total1 < total2:
    print ('Product one is of better value.')
  elif total2 < total1:
    print ('Product two is of better value.')
  else:
    print ('Both products are of equal value.')
MYW
  • 31
  • 1
  • 7
  • `if yes == 'y' or 'yes':`??? you miss something – GAVD May 19 '22 at 06:28
  • So, you have the condition `yes == 'y' or 'yes'`, and you expect that `'n'` will fail the condition. Correct? So, why should it? Talk through step by step your reasoning. I assume you said something like: "well, `'n'` isn't `'y'`, and it also isn't `'yes'`". Correct? So - *why should that code compare the variable `yes` to the string `'yes'`? It doesn't say `yes == 'yes'` anywhere, does it? On the right-hand side of the `or`, there is only `'yes'`. – Karl Knechtel May 19 '22 at 06:31
  • @KarlKnechtel ah ok that makes so much sense. Thank you! – MYW May 19 '22 at 06:39

0 Answers0