В учебнике «Руководство для начинающих по Python 3» в главе 11 приведен пример функции. Программа:
def get_integer_input(message):
"""
This function will display the message to the user
and request that they input an integer.
If the user enters something that is not a number
then the input will be rejected
and an error message will be displayed.
The user will then be asked to try again."""
value_as_string = input(message)
while not value_as_string.isnumeric():
print("The input must be an integer greater than zero.")
value_as_string = input(message)
return int(value_as_string)
age = get_integer_input("Please input your age: ")
age = int(age)
print("age is", age)`
Выходные данные, согласно учебнику, должны быть:
Please input your age: 21
age is 21
Но я получаю:
Please input your age: 20
Traceback (most recent call last):
File "/Users/RedHorseMain/Documents/myPythonScripts/A Beginners Guide to Python 3/6.10.3 getAge.py", line 20, in <module>
age = int(age)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Однако, если я сначала ввожу строку вместо целого числа, ошибка, от которой должна защищать функция, работает:
Please input your age: Red
The input must be an integer greater than zero.
Please input your age: 21
age is 21
Кто-нибудь, пожалуйста, объясните, почему функция возвращает NoneType?