中断并重新启动 python 脚本

Interrupting and restarting a python script

假设我有一个如下所示的脚本

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

有什么方法可以中断 while 循环并 return 到要求用户输入的部分?

假设您指的是 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