'if statement not working properly for 3 numbers
first of all i don't have any intention in making this code big, I know there are ways to cut it short but i wanted to know why this specific code is not working.
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 for the rest
Solution 1:[1]
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.
Why your code 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")
Solution 2:[2]
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" )
Solution 3:[3]
print(max([a, b, c, d, e]), 'is the largest')
"max" is a Python built-in function that can return the biggest item. You can pass in multiple args or an iterable (i.e. a list of numbers).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | wovano |
| Solution 3 |

