0
def working_with_three(num1, num2):
    if num1 or num2 == 3 and 3 in str(num1 + num2):
        return True
    else:
        return False

working_with_three(-6, 19)

I am trying to create a function that returns True only if one of the numbers is 3 AND the sum contains 3, but when I run this I keep getting True, even though I should be getting False

Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
  • 2
    This is a very common question (so perhaps should be closed as a duplicate) but not easy to search for. You really want `num1==3 or num2==3` since all `and` and `or` do is link together different things that can be true or false. – Andrew Jaffe Jul 03 '21 at 10:37
  • 1
    ```3 in str(num1 + num2)``` will give you an error. You are checking for the presence of an ```int``` in a ```str```. Use ```'3' in str(num1 + num2)```. – Ram Jul 03 '21 at 10:41
  • Also: Since you convert `num1 + num2` to a string, you must check whether `'3'` is contained in this string, not the integer `3`. – Pythocrates Jul 03 '21 at 10:42

2 Answers2

0
def working_with_three(num1, num2):
sum1 = num1+num2
if ((num1 ==3 or num2==3) and str(3) in str(sum1)):
    return True
return False
  • Thank you all for you insights. Especially on the beginner's issues highlighted. This notes will serve me very well going for. The issue is now resolved. – Sifiso Mabaso Jul 03 '21 at 11:06
  • 1
    @SifisoMabaso I think you probably meant this comment on a different answer... You should at least up-vote, and, if you have enough reputation, select an answer! – Andrew Jaffe Jul 03 '21 at 11:17
0

So, there are a number of interesting "beginners'" issues here.

First num1 or num2 == 3 is evaluated as (num1) or (num2==3) and then we have to figure out how to convert (num1) into something that is true or false. It so happens that in python, the number 0 is false, and all other numbers are true.

So what you really want is num1==3 or num2==3.

You also need to think about the other part of the if: you want to check whether the character "3" is in the string you made from the number, not whether the number 3 is in the string (which doesn't even really make sense). So '3' in str(num1 + num2).

But wait, there's more. Whenever you see the pattern

if something:
    return True
else:
    return False

you should think deeply about what's happening: something is turning out to be True or False and the routine is then returning True or False. Which means you can instead just do

return something
Andrew Jaffe
  • 26,554
  • 4
  • 50
  • 59
  • To make `return something` equivalent to the `if` statement, you would need to write `return bool(something)` to capture its ["truthiness"](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false). – martineau Jul 03 '21 at 10:52
  • Well, it depends on whether the point of the routine is really to return a bool or just something with that "truthiness" -- and in this case, of course, it actually is a bool. – Andrew Jaffe Jul 03 '21 at 11:15
  • I was merely pointing out that the assertion "…you can instead just do" is incorrect. – martineau Jul 03 '21 at 11:19