Reputation: 13
How can i make the function return an int and not None in this code.?
def num():
try:
user_input = int(raw_input('Enter an number:'))
return user_input
except:
print 'PLZ enter an number'
num()
print num()
Upvotes: 1
Views: 31
Reputation: 76194
Recursively calling a function inside itself doesn't cause the inner call's return value to be returned by the outer call. You need to explicitly return it.
def num():
try:
user_input = int(raw_input('Enter an number:'))
return user_input
except:
print 'PLZ enter an number'
return num()
print num()
Incidentally, using recursion to ask the user repeatedly for input will cause the call stack to reach its maximum depth and crash if the user enters an incorrect value 99 times in a row. See Asking the user for input until they give a valid response for more information.
Upvotes: 1