繁体   English   中英

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

[英]if statement not working properly for 3 numbers

从用户处输入五个数字

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

if 分支仅在 a 和 b 具有最大值而其余部分不起作用时才有效

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

兄弟,如果你想最大限度地使用这些,如果你想简单地做到这一点,你应该使用

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

你的代码缩进是错误的。 您应该在 1 行中添加您的条件。 让我们假设 b>a 和 c>b 。 第一个 elif 条件返回 true b>a 。 但是 elif 条件 b>c 的第二个缩进是假的。 所以整个 if 条件不返回任何内容导致第二个 elif 返回 true 并且缩进返回 false 。 所以其他 elif 和 else 范围不起作用。 你的代码应该是这样的:

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