Home > Enterprise >  python program for finding greater number among of 4 numbers, using nested if
python program for finding greater number among of 4 numbers, using nested if

Time:11-13

WHAT IS WRONG IN THIS CODE? MY PC SHOWS NO OUTPUT WHEN C and D ARE LARGER NUMBERS?

a=int(input("ent a no."))
b=int(input("ent a no."))
c=int(input("ent a no."))
d=int(input("ent a no."))

if a>b:
    if a>c:
        if a>d:
            print(" a is greater")
            
            
elif b>a:
    if b>c:
        if b>d:
            print("b is greater")

elif c>a:
    if c>b:
        if c>d:
            print ("c bada hai bc")

else:

    print("d is greater") 

This program shows output when A and B variables have larger number but do not show any output when D and C have larger numbers respectively?

CodePudding user response:

Your final else won't get called if c > a but also wont print if c < d. This is also in pretty bad form, you may to structure it like this:

if a > b and a > c and a > d:
    print("a is greater"

    .
    .
    .

CodePudding user response:

Let's say the numbers you enter are 1, 2, 3, 2. In that case b is greater than a so the second elif in your code is never considered.

A more succinct approach could be:

a = int(input("ent a no. "))
b = int(input("ent b no. "))
c = int(input("ent c no. "))
d = int(input("ent d no. "))

print(max((a, 'a'), (b, 'b'), (c, 'c'), (d, 'd'))[1], 'is greater')
  • Related