简体   繁体   中英

if statement not working properly for 3 numbers

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

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

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" )
         

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM