Repeat python function and return value

I try to repeat the function if certain criteria are not met. For instance:

def test(): print "Hello", x = raw_input() if x in '0123456789': return x test() 

In this program, if you enter the number for the first time, it will return the number. If you type a non-number, it will be repeated as desired. However, if you type a few non-numbers, and then a number, it will not return anything. Why is this so?

+4
source share
2 answers

you need to return test() at the end of the function to return the value returned by the actual call to test ().

+8
source

How you have a test call is the wrong way to do this. Each time your program restarts a function, you will use another stack level. In the end, the program will stop (crash), even if the user never enters one of these characters.

 def test(): while True: print "Hello", x = raw_input() if x in '0123456789': return x 
+4
source

Source: https://habr.com/ru/post/1439268/


All Articles