If elif statement not printing half of the program

I am running a code which has to print which is the highest number among the 5 inputed by user. If I input the largest number in first three inputs, it works just fine and prints which is the highest number. But it won't print anything if I input the highest number in input 4 or 5.

I have looked at this for an hour trying to figure out what's wrong but cannot find anything.

a = int(input("ENTER NUMBER 1:"))
b = int(input("ENTER NUMBER 2:"))
c = int(input("ENTER NUMBER 3:"))
d = int(input("ENTER NUMBER 4:"))
e = int(input("ENTER NUMBER 5:"))
if a > b:
    if a > c:
        if a > d:
             if a > e:
              print("Number 1 is the greatest")
elif b > c:
    if b > d:
        if b > e:
            print("Number 2 is the greatest")
elif c > d:
    if c > a:
        if c > e:
            print("Number 3 is the greatest")
elif d > a:
    if d > b:
        if d > e:
            print("Number 4 is the greatest")
elif e > a:
    if e > b:
        if e > c:
            print("Number 5 is the greatest")

Solution 1:

if the inner condition becomes true then the code stops there only.

try putting else conditions for inner if conditions to know where the code has gone wrong.

better to avoid too many conditions(suggestion).

refer below code if you are aware of logical operators.

a = int(input("ENTER NUMBER 1:"))
b = int(input("ENTER NUMBER 2:"))
c = int(input("ENTER NUMBER 3:"))
d = int(input("ENTER NUMBER 4:"))
e = int(input("ENTER NUMBER 5:"))
if (a > b and a > c and a > d and a > e):
    print("Number 1 is the greatest")
elif (b > c and b > d and b > e):
    print("Number 2 is the greatest")
elif (c > d and c > a and c > e):
    print("Number 3 is the greatest")
elif (d > a and d > b and d > e):
    print("Number 4 is the greatest")    
elif (e > a and e > b and e > c):
    print("Number 5 is the greatest")
else:
    print('else case')

Solution 2:

I'm assuming this is Python, but if I'm wrong, this answer should still generally apply.

Looking at the first few lines and consider the input a=5, b=1, c=10, d=1, e=1.

if a > b:
    if a > c:
        if a > d:
             if a > e:
              print("Number 1 is the greatest")
elif b > c:

The first check (a > b) is true (5 > 1), so it will enter that section of the code (and never reach the other elifs). The next check (a > c) is not true, so it will stop before reaching the print statement.

To do the basic idea you are trying to do you would want something more like:

if a > b and a > c and a > d and a > e:
              print("Number 1 is the greatest")
elif b > c and b > d and b > e:
            print("Number 2 is the greatest")
elif c > d and c > a and c > e:
            print("Number 3 is the greatest")
elif d > a and d > b and d > e:
            print("Number 4 is the greatest")
elif e > a and e > b and e > c:
            print("Number 5 is the greatest")

However, even this is probably not correct, since to handle ties you will want an else at the end instead of an elif with conditions. (If all of your variables are the same, none of the > conditions will be true).

As a comment suggested, looping over this and remembering where the highest value is is probably better. Or possibly sorting a list. Things like this will be better if you need to expand this (e.g. if you wanted 100 inputs instead of 5, looping over an array will be almost the same, but you wouldn't want to write hundreds of conditions).

Maybe something like the following would be more maintainable and easier to debug (you can just change ni to set how many elements you want to read and check).

ni = 5
a = []
for i in range(0, ni):
    ele = int(input())
    a.append(ele)
b = None
for i in range(0, ni):
    if b == None or b < a[i]:
        b = a[i]
        c = i
print("Number " + str(c+1) + " is the greatest")

Edited to add: If all you are ever doing with the values is checking for the max, then you don't even need to store them at all. You could just check them as they come in.

ni = 5
b = None
for i in range(0, ni):
    ele = int(input())
    if b == None or b < ele:
        b = ele
        c = i
print("Number " + str(c+1) + " is the greatest")