Largest number finding out of 3 numbers - if condition

Hi All,

I have a question for the below snippet. I am able to execute the to find the largest number out 3 numbers.

My question is: the first “if” condition is false, what integer stores in variable “large” to excute 2nd if condition?

number1=int(input(“Enter 1st number”))
number2=int(input(“Enter 2nd number”))
number3=int(input(“Enter 3rd number”))
large=int()
if number2>number1:
large=number2
if large<number3:
large=number3
if number1>large:
large=number1
print(“Large number is”, large)

large = int() means the same as large = 0, so most people will write 0.

One tip for figuring out these sorts of things is that you could try adding print statements like this:

number1 = int(input("Enter 1st number"))
number2 = int(input("Enter 2nd number"))
number3 = int(input("Enter 3rd number"))
print("numbers:", number1, number2, number3)

large=int()
print("starting value of large:", large)

if number2>number1:
    print("number2 > number1")
    large=number2
    print("now large = ", large)

if large<number3:
    print("large < number3")
    large=number3
    print("now large = ", large)

if number1>large:
    print("number1 > large")
    large=number1
    print("now large = ", large)

print("Large number is", large)

You might figure out that you can get away with only two if statements, rather than three:

number1 = int(input("Enter 1st number"))
number2 = int(input("Enter 2nd number"))
number3 = int(input("Enter 3rd number"))

large = number1
if number2 > number1:
    large = number2
if number3 > large:
    large = number3

print("Large number is", large)

If you’re trying to understand if-statments, that’s a great thing to be learning. But you could also opt to use the built-in max() function in this case:

number1 = int(input("Enter 1st number"))
number2 = int(input("Enter 2nd number"))
number3 = int(input("Enter 3rd number"))

large = max(number1, number2, number3)

print("Large number is", large)

Dennis,
Thank you very much for your reply!! The tip which you have given to figure out the things is awesome!! I will use this tip in my future learning

In the real world, you’d always want to use max() for this, of course.

However, if you’re required to implement this for an assignment (as I assume you are), as an alternative to writing your own complicated logic that is difficult to reason out, easy to make mistakes on, requires lots of print()s to get right and doesn’t scale with arbitrary numbers of given values, consider an approach with a loop instead that does the same comparison each time.

Let us suppose we have

numbers = [1, 42, 5, ...]

Then, we can find large by executing the following:

large = 0
for number in numbers:
    if number > large:
        large = number

This is shorter, simpler, more idiomatic and much easier to reason with than a chain of if statements—for each number, if it is larger than the largest number yet encountered, it is the new largest number. Plus, it works for any length of numbers without changing the code.