0

I've done this before but i think this error is showing up because I'm not looping the code, the code works only once then on the second attempt is shows an error.

My Code:

import string
import time
def timer(x):
    for n in range(x,0,-1):
        time.sleep(1)
        print(n)
    print("Times Up"+"\n")
    ask("Time for: ")

def ask(a):
    x=int(input(str(a)))
    print("\n"+"Clock's Ticking")
    timer(x)
try:
    ask("Time for: ")
except ValueError:
    ask("Enter a number to time: ")

I want my code to not error when i put in something that isnt a integer for the time but dont know how to loop the exception code until the user enters a integer.

1 Answers1

0

Move the exception handling to ask function:

import string
import time
def timer(x):
    for n in range(x,0,-1):
        time.sleep(1)
        print(n)
    print("Times Up"+"\n")
    ask("Time for: ")

def ask(a):
    x = None
    while x is None:
        try:
            x=int(input(str(a)))
        except ValueError:
            print('Enter a number to time!')
    timer(x)

ask("Time for: ")
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91