0

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

In the first test why does entering (9,9) and (8,3) return True?

I have since solved the problem using a different solution (further below) but I am struggling to understand why the first solution does not work since neither integer is 10 nor do they equal 10 when summed together!

Thanks for your help.

def makes10(a, b):                                        

    if a or b == 10:
        return True

    if a + b == 10:
        return True

    else:
        return False

def makes10(a, b):

    if a == 10:
        return True

    if b == 10:
        return True

    if a + b == 10:
        return True

    else:
        return False
gtlambert
  • 11,711
  • 2
  • 30
  • 48
Gav Mac
  • 21
  • 5
  • 3
    `a or b == 10` means `(a != 0) or (b == 10)`. The construct you wanted is `a == 10 or b == 10`. Is that enough for you to understand your mistake? – zwol Mar 22 '16 at 14:16
  • Duplicate of [this](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – timgeb Mar 22 '16 at 14:18
  • Ah, yes. @timgeb: that is a much better duplicate than the one I picked. Variations of this question have been asked so many times. Thanks! – Bryan Oakley Mar 22 '16 at 14:18
  • So in this case "a" could be any possible number? – Gav Mac Mar 22 '16 at 14:19
  • 1
    Aside: `return (a == 10) or (b == 10) or (a+b == 10)` may be more readable. – P.P Mar 22 '16 at 14:19
  • It was difficult to find answer to this Bryan. I did try :) – Gav Mac Mar 22 '16 at 14:19
  • This is the proposed solution: def makes10(a, b): return (a == 10 or b == 10 or a+b == 10) – Gav Mac Mar 22 '16 at 14:21

5 Answers5

3

It says a or b == 10, since a is true, i.e not zero it returns true.

uSeemSurprised
  • 1,826
  • 2
  • 15
  • 18
1

Because this:

if a or b == 10:

is executed as the equivalent of

temp = (a or b)
if (temp == 10):

YOu can NOT test multiple values against a single fixed value like that. You have to test each one individually, e.g. if you had

if (a == 10) or (b == 10):

the statement would have worked. Read the docs: https://docs.python.org/3/reference/expressions.html#operator-precedence

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 2
    I think `a` evaluates to True and `b` is not evaluated. – Jérôme Mar 22 '16 at 14:18
  • yes. but `or` still has a higher priority than `==`, so you're testing the result of the `or` against `10`, not the individual `a` or `b` values. – Marc B Mar 22 '16 at 14:19
1
a or b == 10

This evaluates to True as a evaluates to True

You must write

(a == 10) or (b == 10)
Jérôme
  • 13,328
  • 7
  • 56
  • 106
1
if a or b == 10:

That doesn't do what you think it does. It will be evaluated as:

if (a) or (b == 10):

You most likely want:

if a == 10 or b == 10:
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1
a or b == 10

This will first evaluate a in the context of a boolean expression, and if this is true the whole expression is just true. If it is false, then it will evaluate b == 10. It is different from a == 10 or b == 10.

Alternatively, you would just say

if 10 in [a, b]:

this will also do what you want to accomplish.

Selçuk Cihan
  • 1,979
  • 2
  • 17
  • 30