Python Forum

Full Version: [easy] If, and.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I'm a new user of Python and I'm learning from the beginning.
I'm trying to make a code that ask for three numbers and show the bigger.
I've started with "if" and "and" to test, but it doesn't work well...follow the code:

n1=int(input("N1: "))
n2=int(input("N2: "))
n3=int(input("N3: "))
if n1 > (n2 and n3):
  print(n1)
In this case I wrote just for the variable n1 (only for test), but when I put N1=5; N2=10 and N3=1, for example, it return "5", but in my opinion the code shouldn't return anything because 5 isn't bigger than 10.

Anybody could help me?

Thanks in advance!
Henry
You need to write your condition like this:
if n1 > n2 and n1 > n3):
you can check what (n2 and n3) in your case returns:
Output:
>>> n2 and n3 1
(Jul-17-2018, 02:19 PM)mlieqo Wrote: [ -> ]You need to write your condition like this:
if n1 > n2 and n1 > n3):
you can check what (n2 and n3) in your case returns:
Output:
>>> n2 and n3 1

Thank you for your answer!
You want to check the other numbers also
n1=int(input("N1: "))
n2=int(input("N2: "))
n3=int(input("N3: "))
for num in [n1, n2, n2]:
    ## a number will never be greater than itself
    ## so there is no harm in comparing it to itself
    if num >= n1 and num >= n2 and num >= n3:
        print(num)
(Jul-17-2018, 06:43 PM)woooee Wrote: [ -> ]You want to check the other numbers also
n1=int(input("N1: "))
n2=int(input("N2: "))
n3=int(input("N3: "))
for num in [n1, n2, n2]:
    ## a number will never be greater than itself
    ## so there is no harm in comparing it to itself
    if num >= n1 and num >= n2 and num >= n3:
        print(num)

Nice! Thank you woooee!
Henry.
Quote:
n1=int(input("N1: "))
n2=int(input("N2: "))
n3=int(input("N3: "))
if n1 > (n2 and n3):
  print(n1)

When in doubt, break it into smaller pieces. What's happening here, is that (n2 and n3) is the last Truthy value, because they're all Truthy. Since you're not comparing them to anything, you're just checking that both of them are not zero. And because none of them are Falsey, the last Truthy value is chained in the expression. So we can rewrite your if like so: if n1 > n3:.

>>> n1 = 5
>>> n2 = 10
>>> n3 = 1
>>> n2 and n3
1
>>> n1 > (n2 and n3)
True
>>> n1 > n2 and n1 > n3
False