You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Python脚本指定代码段执行完操作后自动循环?

实现方案

要实现指定位置循环的需求,只需要把POINT1开始的所有交互逻辑包裹在无限循环结构中即可,初始启动代码放在循环外保证仅开机时执行一次。

完整可运行代码如下:

import time

# 初始启动代码 仅执行一次
print("Booting System (Alfa V1.1)")
time.sleep(1)
print("Hello, and welcome to Training OS")
time.sleep(2)
print("What would you like to do ?")
time.sleep(1)

# 无限循环包裹POINT1之后的所有逻辑
while True:
    # 此处为POINT1位置 每次循环都会从这里开始执行
    txt = raw_input("1.File Explorer  2.System Info.  ( 1 or 2 )")
    if txt == "1": 
        print("Loading File Explorer") 
        time.sleep(1)
        print("  _____    _____ \n /     |  /     | \n |     |  |     | \n |_____|  |_____| \n  File1    File2  ")
        time.sleep(0.5)
        file = raw_input("1.File1 2.File2 ( 1 or 2 )")
        if file == "1":
            print("\n\nFile1:""\n_______________________________________""\nTOP SECRET UPDATE INFO:""\n           Update Name . . . . 1.2""\nEstimated Release Date . . . . 25.09.21""\n           New Features . . . . Games?""\n_______________________________________")
        else:
            print("\n\nFile2:""\n----------------------------------------""\n                                        ""\n              Error 404.                ""\n             File Missing               ""\n                                        ""\n----------------------------------------")
    else:
        print("\n\nSystem Info:""\n--------------------------------------------------------------------------------------""\n\nOs version: . . . . . 1.1 (New features: File Explorer)""\n   Founded: . . . . . 22.09.2021, Baku, Azerbaijan""\n  Products: . . . . . TOS""\n\n***This OS is still being developed so some features are currently unaviable :(***""\n--------------------------------------------------------------------------------------")

补充说明

  • 如果你使用的是Python 3.x版本,将代码中所有raw_input替换为input即可正常运行
  • 需要终止程序时按Ctrl+C即可强制退出

内容的提问来源于stack exchange,提问作者Manuel Pozzoni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 20:39:05