代码之家  ›  专栏  ›  技术社区  ›  Eren

Python递归何时返回[duplicate]

  •  0
  • Eren  · 技术社区  · 3 年前

    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 .这是哪里 没有一个 来自和如何修复我的功能?

    0 回复  |  直到 5 年前