Why isn't this simple code working, for Python? -
x = raw_input("write number") if x.isalpha(): print "invalid!" elif x%2==0: print "the number have written even" elif x%2!=0: print "the number have written odd" else: print "invalid!"
it supposed check if number odd or , print out. if statement checks if raw_input alphabet because won't work. , elif statements check odd or even.
the homecoming value of raw_input
string. you'll need convert integer if want utilize %
operator on it:
x = raw_input("write number") if x.isalpha(): print "invalid!" x = int(x)
instead of x.isalpha()
can utilize exception handling instead:
try: x = int(raw_input("write number")) except valueerror: print 'invalid!' else: if x % 2 == 0: print "the number have written even" else: print "the number have written odd"
because int()
raise valueerror
if input not valid integer.
python python-2.7
No comments:
Post a Comment