0

help - except?

hi this is my code: def x(n): result = 1 while n > 0: result = result * n n -= 1 return result while True: print('input a positive number ') i=input() if i=="exit": print('quitter!') break # elif try: # int(i) # except ValueError # print('number! not word!') elif int(i) >= 0: n=int(i) print(n, ' factorial is ', x(n)) break else: print('must be positive') please look at commented part i dont know how to complete it correctly i want to print "number! not word!" if input is not "exit" nor integer type im looking for solution or maybe i should use sth different than "except" im sure there are other ways to do it

29th Dec 2016, 11:14 AM
Brt
2 Answers
+ 2
def x(n): result = 1 while n > 0: result = result * n n -= 1 return result while True: print('input a positive number ') i=input() if i=="exit": print('quitter!') break try: n = int(i) except ValueError: print('number! not word!') continue if n >= 0: print(n, ' factorial is ', x(n)) break else: print('must be positive')
28th Dec 2016, 5:24 PM
harihara
harihara - avatar
+ 1
thx a lot ;) now i see i was close
28th Dec 2016, 5:31 PM
Brt