[英]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.