简体   繁体   English

Python 中的无限循环随机退出

[英]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.我相信我在 Python 3.8.5 上并且正在使用 Jupyter 笔记本。 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.我无法确定确切的原因是什么,例如,它可以通过输入 1 2 2 1 3 2 <- 来工作,然后它只是随机停止。 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. '-1' 输入正常工作,并会按预期停止循环。

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 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. clear_output 的参数 wait = True 等待清除 output 直到有新的 output 可以替换它。 This was causing the code to crash unexpectedly, switching to clear_output(False) seems to fix this issue.这导致代码意外崩溃,切换到 clear_output(False) 似乎可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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