0

Let's say that I have a script that looks like this

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

Is there a way that I can interrupt the while loop and return to the part that asks for user input?

Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
user498823
  • 45
  • 4

1 Answers1

1

Assuming you are referring to a 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
Or Y
  • 2,088
  • 3
  • 16