Quantcast
Viewing latest article 3
Browse Latest Browse All 3

Possible to raise two types of errors?

So I'm experimenting making my own program. I have the user input a string and an integer (name, age).

I want to raise a Value Error if the age is under 1 (if age > 1:) I did that. But I'm not sure what to do if the name is not a string. Is it a TypeError and can two types of errors be raised at the same time? If so how?

Probably got some terminology wrong but having trouble thinking right now.

Here's the code:

# This program asks name how old you are and makes exceptions to check and see if 
there are errors

def hogwarts_express (name, age):

    if age < 1:

        raise ValueError ("Error: Apparently you don't exist.  Please pick a number older 
than 0!")

    if int (age) >= 10:
        print ("Hello {}!  Welcome to the Hogwarts Express, your old enough to go now.  
Here 's your ticket!".format(name))
    else:
        print ("Sorry {} you're not old enough to board the express.".format(name))
try:
    your_name = input("What's your name?  ")
    age = int(input("How old might you be?  "))
    together = hogwarts_express (your_name, age)

except ValueError as err:
    print ("That's not a valid value.  Please input something else.")
    print ("{}".format(err))

else:
    print (together)

Viewing latest article 3
Browse Latest Browse All 3

Trending Articles