中断并重新启动python脚本

2024-10-06 13:03:09 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个这样的脚本

myInput = input(int('Enter a number: '))
while myInput > 0:
    print(myInput)
    myInput-=1

有没有一种方法可以中断while循环并返回到请求用户输入的部分


Tags: 方法用户脚本numberinputintprintenter
1条回答
网友
1楼 · 发布于 2024-10-06 13:03:09

假设您指的是KeyboardInterrupt

def foo():
    try:
        myInput = int(input('Enter a number: '))
        while myInput > 0:
            print(myInput)
            myInput-=1
        return True
    except KeyboardInterrupt:
        return False

while not foo():
    pass

相关问题 更多 >