Jan-27-2023, 01:32 PM
(This post was last modified: Jan-27-2023, 05:22 PM by deanhystad.)
Fixing your program is not all that simple. Your program has a design flaw that will eventually result in a recursion limit exceeded exception. This would take a while, so let's speed things up a bit.
This does the same thing as above, but it runs forever without consuming more and more resources.
import random def stop(): main() def Left(): stop() def Middle(): stop() def Right(): stop() def main(): random.choice((Left, Middle, Right))() main()
Error: File "...", line 7, in stop
main()
File "...", line 19, in main
random.choice((stop, Left, Middle, Right))()
blah, blah, blah calling some function that calls main that calls another function that calls stop() that calls main ... until eventually
File "C:\Program Files\Python311\Lib\random.py", line 239, in _randbelow_with_getrandbits
k = n.bit_length() # don't use (n-1) here because n can be 1
^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while calling a Python object
You should use a loop in main() instead of using recursion. main() should call Left(). Left() should do it's thing and call stop. stop() should do it's thing and end, letting control return to Left(). Left() should execute any remaining code (currently none) and end, letting control return to main().This does the same thing as above, but it runs forever without consuming more and more resources.
import random def stop(): pass def Left(): stop() def Middle(): stop() def Right(): stop() def main(): while True: random.choice((Left, Middle, Right))() main()