1

I'm starting to learn Python and wrote this:


num1 = int(input('First number '))
num2 = int(input('second number '))
num3 = int(input('third number '))

if num1 > (num2 and num3):
    print(f'{num1:2} is bigger')
    if num2 > num3:
        print(f'{num3:2} is smaller')
    if num3 > num2:
        print(f'{num2:2} is smaller')

if num2 > (num1 and num3):
    print(f'{num2:2} is bigger')
    if num1 > num3:
        print(f'{num3:2} is smaller')
    if num3 > num1:
        print(f'{num1:2} is smaller')
    

if num3 > (num1 and num2):
    print(f'{num3:2} is bigger')
    if num1 > num2:
        print(f'{num2:2} is smaller')
    if num2 > num1:
        print(f'{num1:2} is smaller')

If I enter "5" for the first number "4" for the second and "2" for the third it outputs:

"5 is bigger
 2 is smaller
 4 is bigger
 2 is smaller"

Why does he reads the line with the false condition statment?

What did I miss?

Also another way to write it would be:

"if num2 > (num1 and num3) and (num1 < num3):
    #print(f'{num2:2} is bigger and {num1:2} is smaller')"

but also didn't work.

Thanks everyone.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    `num1 > (num2 and num3)` does not do what you think it does. Rather than explain what exactly it DOES do, I'll just say that I think you want to say `if num1 > num2 and num1 > num3` – Cresht Dec 15 '21 at 23:19
  • I suggest you do `print(num2 and num3)` to see what you're actually comparing with. Logical operators don't automatically distribute. – Barmar Dec 15 '21 at 23:20
  • You should also use `elif` when the conditions are mutually exclusive. And use `else:` in place of the last condition. – Barmar Dec 15 '21 at 23:21

2 Answers2

0
num1 > (num2 and num3)

That doesn't work how you think it does, you need something like:

num1 > num2 and num1 > num3

# Or if there's lots of them:
num1 > max(num2, num3, num4, num5)

Your current method first calculates num2 and num3 by giving you num2 if it's false (see below), or num3 otherwise. So 3 > (4 and 2) is the same as 3 > 2, because 4 and 2 is 2. The following transcript illustrates this:

>>> print(0 and 99)
0
>>> print(1 and 99)
99
>>> print(2 and 99)
99
>>> print(-1 and 99)
99

Basically, numbers are considered false if they're zero, hence the output shown above.


As an aside, you may also want to think about what will happen if two or more of the numbers are equal.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
-1
num1 > (num2 and num3)

This thing does not work the way your are wanting it to be. You need to separate the conditions and then add "and" to it. It should look something like this.

num1 > num2 and num1 > num3

Also, in your code you wrote never used elif or else which is a handy thing. Using that could reduce a some of the conditions and ambiguity.

num1 = int(input('First number '))
num2 = int(input('second number '))
num3 = int(input('third number '))

if num1 > num2 and num1 > num3:
    print(f'{num1:2} is bigger')
    if num2 > num3:
        print(f'{num3:2} is smaller')
    else: # as num2 > num3 is not true, then the opposite one should be true
        print(f'{num2:2} is smaller')

elif num2 > num1 and num2 > num1:
    print(f'{num2:2} is bigger')
    if num1 > num3:
        print(f'{num3:2} is smaller')
    else:
        print(f'{num1:2} is smaller')
    
elif num3 > num1 and num3 > num2:
    print(f'{num3:2} is bigger')
    if num1 > num2:
        print(f'{num2:2} is smaller')
    else:
        print(f'{num1:2} is smaller')
sam5922
  • 51
  • 6
  • You should check that assertion ("... returns a bool value which cannot be compared with num1") with something like `print(4 and 5)`, that is very much *not* a boolean value. It returns the first value if it's truthy, otherwise it returns the second. That just happens to work well for actual boolean values as well :-) – paxdiablo Dec 15 '21 at 23:46