0

I'm trying to make a small program for it to return True if one of the integers is 10 or if their sum is 10. Please see the code.

I've been trying to switch between 'or' or 'and'. But no luck

#Given 2 ints, a and b, return True if one if them is 10 or if their sum 
#is 10.

def makes10(a, b):
  if a or b == 10:
    return True
  elif a + b == 10:
    return True
  else:
    return False

print(makes10(9,10))
print(makes10(9,9))  #This one is not giving False.
print(makes10(1,9))    

I expect the output of print(makes10(9,9)) to be False

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Robert Grootjen
  • 197
  • 2
  • 13

1 Answers1

0

In python, or is a logical operator.

If any of the two operands are non-zero then condition becomes true.

Your input values (1, 9 and 10) are non-zeros, so the expression a or b == 10 is always true.

As suggested by Primusa, you should first evaluate if your inputs are equal to 10 before using the logical operator or.

def makes10(a, b):
  if a == 10 or b == 10:
    return True
  elif a + b == 10:
    return True
  else:
    return False
Guillaume Adam
  • 191
  • 2
  • 10