Python: Return Value and Return None

后端 未结 1 1170
失恋的感觉
失恋的感觉 2021-01-21 20:02

I can\'t figure out why the function returns the correct letter when I enter \"N\" or \"n\". The function is called but return \"None\" when I enter the incorrect letter. The fu

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 20:24

    With auswahl() you are just calling your function recursively, but do nothing with the value it produces.

    It would have to be return auswahl().

    However, note that using recursion in functions that take user input is considered harmful because you can blow the stack if a user fails too many times. See the "Common Pitfalls" section of the answer I linked to.

    ~edit~

    But if i put a return there, it gets back to main?! With recursion you mean that the function calls itself?

    Yes, recursion in this contexts refers to a function calling itself. return auswahl() does not immediately return from the function, it has to wait for the result another call to auwahl produces. Of course, in that other call the user could fail again, which would trigger another call, and so on ...

    0 讨论(0)
提交回复
热议问题