+ 1

Why is it showing error when i inserted the values - x= 2, y=1 and z=5?

https://code.sololearn.com/c2ros1v3EzxN/?ref=app

30th Nov 2020, 12:22 PM
Anve
Anve - avatar
3 Antworten
+ 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
30th Nov 2020, 3:12 PM
Lothar
Lothar - avatar
+ 3
Anve , only one part of a if / elif / else condition can be executed. Please read my comments in my previous post.
30th Nov 2020, 3:35 PM
Lothar
Lothar - avatar
+ 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...
30th Nov 2020, 12:46 PM
Jayakrishna 🇮🇳