I need help with this script as i dont know why exactly the second <elif> is not read...keep informed that i am still at this point of conditional statments chapter of the course i am attending

largest_number = 0
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))

if number1 > number2:
if number1 > number3:
largest_number = number1

elif number2 > number1:
if number2 > number3:
largest_number = number2

elif number3 > number1:
if number3 > number2:
largest_number = number3

else:
print(“error”)

print(“The largest number is:”, largest_number)

Please annotate your code (make the line above and below the code have three backticks). Without it, the indentation can’t be seen, which is important.
Please tell us what input you think should trigger that codpath, what output you expect for it, and what output you are getting instead.

1 Like

Hi Ziadmohamedezzat, welcome!

Sorry, I don’t understand what you mean by “the second is not
read”.

Can you write a program that finds the largest of two numbers? If you
can do that, then you can

  • find the larger of number1 and number2

  • then find the larger of number3 and largest_number

and that’s the largest of the three numbers.

“i am trying to write a program that finds the largest of 3 number with different methods, i tried to script it with this way” but everytime i enter the third number value; i get 0 for the <largest_number> Variable

i am trying to write a program that finds the largest of 3 number with different methods, i tried to script it with this way" but everytime i enter the third number value; i get 0 for the <largest_number> Variable

Line 1 sets largest_number to 0. For that input, your logic never resets it, so it reports zero. It might be better to get rid of that first line and just have your program generate an error rather than a wrong number.

You have 3 if/elif/elif stanzas. Only one can be entered. Since 2 > 1, you enter the second stanza. But then 2 is not bigger than 3, so the largest_number is not reset. The if/elif/elif is complete and you drop to the print line, never resetting the largest number.

The second elif isn’t entered because the first elif is entered. You can only enter one.