我有这个调用自己的函数:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
现在,如果我只输入“a”或“b”,一切正常:
Type "a" or "b": a
got input: a
但是,如果我输入其他内容,然后输入“a”或“b”,我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
我不知道为什么get_input()
要返回None
,因为它应该只返回my_var
。这是None
从哪里来的,我该如何修复我的功能?