【发布时间】:2019-02-05 14:32:25
【问题描述】:
我有一个要求一些特定数据的函数。我有另一个接受输入的函数,如果它应该是它应该是什么,它会分配给它被调用的变量。
问题是如果我得到一个错误的值,那么它总是会返回NoneType 对象,即使我第二次得到了很好的答案。
我尝试删除clear()。我试图增加更多的回报,即使答案不应该通过。我尝试使用要分配值的变量的名称向函数添加第二个参数
def ask_input_string(text):
''' Takes only one argument. This function asks for input when it is called and returns the value but with the argument provided as text. '''
variable=input(text)
if variable=="":
print("\nField must not be empty")
ask_input_string(text)
try:
int(variable)
print("\nPlease enter a name!")
ask_input_string(text)
except ValueError:
return variable
def menu_add():
clear()
print("\nPlease add the data required!\n")
nume=ask_input_string("Name of animal: ")
animal_type=ask_input_string("\nAnimal Type: ")
预期结果:
如果我添加了错误的值,它会要求正确的值。我给出了正确的,它分配给它被调用的变量。
实际结果:
如果我至少给出了 1 次错误答案,那么即使我得到正确答案,它也会始终将 NoneType 分配给调用它的变量。
如果我第一次给出正确答案,它会起作用。如果我首先给出错误的答案,它将不起作用。
【问题讨论】:
标签: python python-3.x function recursion nonetype