简体   繁体   English

if 语句对 3 个数字不能正常工作

[英]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 if 分支仅在 a 和 b 具有最大值而其余部分不起作用时才有效

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.您应该在 1 行中添加您的条件。 Let's assume b>a and c>b .让我们假设 b>a 和 c>b 。 First elif condition is returns true b>a .第一个 elif 条件返回 true b>a 。 But second indent of elif condition b>c is false.但是 elif 条件 b>c 的第二个缩进是假的。 So whole if condition returns nothing cause second elif returned true and indent is returned false.所以整个 if 条件不返回任何内容导致第二个 elif 返回 true 并且缩进返回 false 。 So other elif and else scopes is not worked.所以其他 elif 和 else 范围不起作用。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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