def foo():
try:
html = get(url)
while (html is nogood):
foo()
return html
except Exception as e:
changeip()
foo()
The function foo simply returns a good html in text.
However, the function sometimes returns None and it's caused
when an Exception is caught.
So I changed the last line to return foo() and it works as expected.
The question is why? On catch, it simply calls foo again, which would eventually return a good html in text, why do I have to put the extra return?
Thanks
You don't need a recursion solution at all. In foo, just keep looping until you have a good html. After each time through the loop, do something to improve the chance to get a good html (does changeip work for this purpose?).
def foo():
while True:
try:
html = get(url)
if is_good(html):
return html
except Exception: # Need specific exception, do not catch all
changeip()
Think about what the first foo() returns if it fails.
Call foo1 from main and it fails -> foo1 calls foo2 and it succeeds -> foo2 returns html to foo1 -> there is no other code to execute in foo1 so it returns None.
When foo finally succeeds, it doesn't return html to main, it returns it to the last caller, which in this case is foo1.
The problem is that you are discarding the results of any foo call after the first one fails. You need to return html back down the chain by the last line to return foo() as you mentioned.
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