Home > Back-end >  Except function is always executed when I try and give it a specific error
Except function is always executed when I try and give it a specific error

Time:01-07

What I would want this program to do is to only go to each except when the specific error shows up. Right now it will always go to the TypeError exception and will be in a loop asking please give me your number even when I have inputted something.

numb1 = input('Give me a number')

def error(numb1):
    try:
        numb1 >= 0
        print('Your wait time is', 45 / numb1)
    except TypeError:
        numb1 = input('Please give a number')
        return error(numb1)
    except ZeroDivisionError:
        numb1 = input('Do not use zero for your answer. Please input a new number:') 

error(numb1)

CodePudding user response:

This does what I think you were trying for. Note that I use a loop instead of recursion. We break out of the loop when we succeed.

def error(numb1):
    while True:
        try:
            numb1 = int(input('Give me a number'))
            print('Your wait time is', 45 / numb1)
            break
        except TypeError:
            print( "That's not a number." )
        except ZeroDivisionError:
            print('Do not use zero for your answer.') 

error(numb1)
  •  Tags:  
  • Related