+ 1
Why is it showing error when i inserted the values - x= 2, y=1 and z=5?
3 ответов
+ 6
The code does not give an error, but it shows a wrong result. This is because of the structure of the if / elif / else conditions:
... # input x= 2, y= 1, z = 5
max_val = 0
if x >= y: # ---> this condition is True
if x>=z: # ---> this condition is NOT True, so nothing happens and the complete if / elif / else block will be left
max_val = x
elif y>z:
max_val = y
else:
max_val = z
print(max_val) # this the next line of code that will be executed. max_val is still zero and will be printed
+ 3
Anve , only one part of a if / elif / else condition can be executed. Please read my comments in my previous post.
+ 2
Variable Max_value you defining in if blocks so scope is limited to if blocks. So outside it is undefined...
So declare before if - else block and use it in if blocks.
or instead of storing variable, just print there since it is not needed anymore and you have else if blocks...