Home > Enterprise >  if statement not working properly for 3 numbers
if statement not working properly for 3 numbers

Time:11-06

Taking input of five numbers from the user

a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
c=int(input("Enter the value of c:"))
d=int(input("Enter the value of d:"))
e=int(input("Enter the value of e:"))
if a>b:
    if a>c:
        if a>d:
            if a>e:
                print(a,"is the largest")
elif b>a:
   if b>c:
        if b>d:
          if b>e:
                print(b,"is the largest")
elif c>a:
     if c>b:
         if c>d:
              if c>e:
                   print(c,"is the largest")
elif d>a:
     if d>b:
        if d>c:
          if d>e:
                print(d,"is the largest")
else:
    print(e,"is the largest")

The if branching is only working if a and b have the largest values and not working fro the rest

CodePudding user response:

print(max([a, b, c, d, e]), 'is the largest')

CodePudding user response:

bro if you want to max of these you should use if you wanna do it easy way

a = int(input("Enter the value of a:"))
b = int(input("Enter the value of b:"))
c = int(input("Enter the value of c:"))
d = int(input("Enter the value of d:"))
e = int(input("Enter the value of e:"))   
print(max(a,b,c,d,e), "is the largest on these nums" )
         

CodePudding user response:

Your code's indents are wrong. You should add your conditions in 1 line. Let's assume b>a and c>b . First elif condition is returns true b>a . But second indent of elif condition b>c is false. So whole if condition returns nothing cause second elif returned true and indent is returned false. So other elif and else scopes is not worked. Your code should be like this:

a=int(input("Enter the value of a:"))
b=int(input("Enter the value of b:"))
c=int(input("Enter the value of c:"))
d=int(input("Enter the value of d:"))
e=int(input("Enter the value of e:"))
if a>b and a>c and a>d and a>e:
    print(a,"is the largest")
elif b>a and b>c and b>d and b>e:
    print(b,"is the largest")
elif c>a and c>b and c>d and c>e:
    print(c,"is the largest")
elif d>a and d>b and d>c and d>e:
    print(d,"is the largest")
else:
    print(e,"is the largest")
  • Related