简体   繁体   中英

Infinite Loop in Python randomly exiting

So I'm constructing some form of a simulator but am experiencing my loop breaking for no reason.

I believe I am on Python 3.8.5 and am using Jupyter notebook. Below is an example of the code causing the problem. I kinda just wanted it to be a user-controlled simulator that goes on until the user is done with it. Currently, it works as expected but randomly shuts down (Kernal still running it seems). I unable to identify what is the exact cause, it could work for example by type inputs 1 2 2 1 3 2 <- then it just randomly stops. I realized spamming invalid inputs causes it to randomly exit as well. Not sure what other debugging techniques I could make use of here. :(

'-1' input works normally and would stop the loop as intended.

I initially ignored it making it such that the code is continuable by just interrupting and rerunning it, but have decided to try to find out what is causing it.

I'm not sure if I have made any stupid mistake or have the logic wrong, any advice would be greatly appreciated. Thanks in advance.

# Run simulation, when ended, simulation will continue from last run.
response = ""
main_input = ""
sec_input = ""

while True:
    print("=== Welcome to my Simulator ===")
    print("Current simulation date is ", simulation_date)
    print('''
1) Predict today's prices
2) Go to next day
3) Fast forward X days
4) Jump to X date
-1) End simulation
''')
    print(response)
    main_input = input("What would you like to do? ")
    
    if main_input == '1':
        response = "Today's prediction is ~"
        
    elif main_input == '2':
        response = "Going to next day..."
        
    elif main_input == '3':
        response = "Fast forwarding..."
        
    elif main_input == '4':
        response = "Jumping to X date"
        
    elif main_input == '-1':
        print("Simulation paused, hope you see you back soon! uWu")
        break;
    else:
        response = "Invalid Option"
    clear_output(True)

Edit* Example of how it looks when it just randomly exits随机退出示例

https://ipython.org/ipython-doc/3/api/generated/IPython.display.html

clear_output's parameter wait = True waits to clear the output until a new output is available to replace it. This was causing the code to crash unexpectedly, switching to clear_output(False) seems to fix this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM