Feb-22-2019, 10:16 PM
(This post was last modified: Feb-22-2019, 10:16 PM by WatcherMagic.)
I'm a newbie/amateur trying to go back and refresh my basic python after getting too excited and carried away with looking at a few other things. I'm working on a simple python prompt in Jupyter: create a program that asks for an age and then prints out the year they'll be 100 years old.
I'm running into a weird problem that I don't quite get: I'm practicing planning for errors in the code, and if the 'except' clause never runs the code functions properly, i.e returns a value, but if the except clause does run no value is returned.
Here's the code:
and the output:
How old are you?
three
You must enter a number!
How old are you?
3
NoneType
Again, running the code with no error successfully returns a value.
I'm running into a weird problem that I don't quite get: I'm practicing planning for errors in the code, and if the 'except' clause never runs the code functions properly, i.e returns a value, but if the except clause does run no value is returned.
Here's the code:
def askAge():
age = input("How old are you?\n")
try:
return int(age)
except:
print('You must enter a number!')
askAge(
and the output:
age = askAge()
How old are you?
three
You must enter a number!
How old are you?
3
type(age)
NoneType
Again, running the code with no error successfully returns a value.