some problems with the exceptions (python)

Viewed 21

when I'm using except (ValueError) I got NameError: name 'age' is not defined, without it the code works well enter image description here I don't want to get an error, i want the code to keep working. if you want the full code tell me

if __name__ == "__main__":

    fname = input(" please enter your first name : ")

    lname = input(" please enter your last name : ")

    try:

        age = int(input('please enter your age : '))

        if age < 17 or age > 25:

            raise AgeError("Age invalid, please enter yor information again")

    except (AgeError) as A:

        print(A.arg1)

    except (ValueError) as X:

        print('your age must be a number!')

    try:

        level = int(input('please enter your level : '))

        if level > 15 or level < 1:

            raise LevelError("Level invalid, please enter yor information again")

    except (LevelError) as L:

        print(L.arg1)

    except(ValueError) as X:

    print('your age must be a number!')

    s1 = Students(fname, lname, age, level)

    print(s1)
0 Answers
Related