I am trying to have a function repeat itself if a certain criteria is not met. For instance:
def test():
print "Hello",
x = raw_input()
if x in '0123456789':
return x
test()
In this program if you type a number the first time, it will return the number. If you type some non-number, it will repeat itself as desired. However, if you type some non-numbers and then a number, it will return nothing. Why is that the case?
you need to return test()
at the tail of the function to return the value that the valid call into test() returns.
The way you are having test call itself is the wrong way to do it. Each time your program restarts the function, you will use up another level of stack. Eventually the program will stop(crash) even if the user never inputs one of those characters.
def test():
while True:
print "Hello",
x = raw_input()
if x in '0123456789':
return x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With