+ 1
x=5 y=8 if x==y: print ('c and y are equal') else : if x<y: print('x is less than y') else :
after if its showing invalid syntax to else why??
3 Respostas
+ 6
If and else statements have to be on the same "level"
Example:
x=5
y=8
if x==y:
print('x and y are equal')
elif x<y:
print('x is less than y')
else:
print('y is less than x')
+ 1
you can keep the same code to just move the if under else a bit right
0
Python distinguishes the different "blocks of code" using indentation. This means that you should be very careful on how you write your code. Here the else statement should be placed exactly at the same level with the "if x==y" statement. In other words, these two statements should start from the same column.