-2

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?

  • 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. Also, what output would you want if the input is 1, 2, 3, 3 ? – DarkKnight Nov 12 '22 at 15:20

2 Answers2

0

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"

    .
    .
    .
CumminUp07
  • 1,936
  • 1
  • 8
  • 21
0

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')
DarkKnight
  • 19,739
  • 3
  • 6
  • 22