jfinizolas jfinizolas - 5 months ago 9
Python Question

Issue with Python function

I am developing some functions, but this one doesn't work and i can't see any reason why:

def f():
t = raw_input('Enter a number and we will send an inormation: ')

if t % 2 == 0:
print t
print 'it is an even number'

elif t % 2 > 0:
print t
print 'it is an odd number'

else t != type(int):
print 'is this a number ?'


it should show the number and the message but when I start the program it says:

def f():
t = raw_input('Enter a number and we will send an inormation: ')

if t % 2 == 0:
print t
print 'it is an even number'

elif t % 2 > 0:
print t
print 'it is an odd number'

else t != type(int):
print 'is this a number ?'

SyntaxError: invalid syntax


The text (^) doesn't show, but the t from the else is highlighted and when erase the else part of the program, the error repeats in the elif BUT when I erase else and elif ( just keep the if) it works. do you know why???

Answer

The else part of an if statement does not have a condition following it. An else catches all executions that were not caught by preceding if and elif lines.

So either remove the t != type(int) condition after the else or change the else to an elif.

Comments