So I’m trying to write a code that will print the highest number and this what I wrote:
a=int(input())
b=int(input())
c=int(input())
if a>b:
if a>c:
print(a)
elif a>c:
if a>b:
print(a)
elif b>a:
if b>c:
print(b)
elif b>c:
if b>a:
print(b)
elif c>a:
if c>b:
print(c)
elif c>b:
if c>a:
print(c)
but for some reason sometimes it just doest print anything and I have no idea why. Also the code can’t contain min, max, and and or. What am I missing?(Sorry for this absolutely awful english btw)
>Solution :
I understand there are much more better way to solve this problem, but I see you as beginner with python, let me try to explain the bad conditional check you are doing here.
The problem with above code is inner and outer conditions with elif/if and if.
(there can be many such scenario with different input but the
conceptually answer is the same.)
let’s say you provided input,
4
5
1
The logical flow goes like this,
a=int(input()) # 4
b=int(input()) # 5
c=int(input()) # 1
Now this condition will failed and cursor will move to next condition.
if a>b: 4 > 5 ( condition false return to next elif)
if a>c:
print(a)
This condition is will be true and cursor reaches to inner if, which will return false and cursor come out of the condition statement printing nothing.
elif a>c: 5 > 1 true
if a>b: 4 > 5 (come out of all the conditional check)
print(a)
elif b>a:
if b>c:
print(b)
elif b>c:
if b>a:
print(b)
elif c>a:
if c>b:
print(c)
elif c>b:
if c>a:
print(c)