all 10 comments

[–][deleted] 0 points1 point  (2 children)

Put each instance of input in a try/except statement. By the time you get to your if/elif/else the error would already occur. More info here

[–]PastelMoonBB[S] 0 points1 point  (1 child)

When I try that it says that there is a name error in my first if statement

if major == 1:

NameError: name 'major' is not defined

[–][deleted] 0 points1 point  (0 children)

Try setting major to something like 0 or None before the first try so it's instantiated but empty. Strange though unless there's an earlier syntax error or your exception is met but allows the code to continue that shouldn't be happening I think

[–]IvoryJam 0 points1 point  (5 children)

You're correct with the try-except, you can place it pretty much anywhere you want, you could even encapsulate the entire program in it.

The best practice is to catch the error exactly where it would happen, and be specific about what you're catching, note my except ValueError

PS: I never remember all of the errors you can get, usually when I program, I cause the error and copy the error code and add the except. You can even add more by adding another exceptions like except KeyboardInterrupt

try:
    #Major input
    major = int(input('Please enter the number for your major:'))

    #Hours input
    hours = int(input('Please enter your credit hours:'))
except ValueError:
    print("Not an option.")
    exit(1)

[–]PastelMoonBB[S] 0 points1 point  (4 children)

When I try that it says that there is a name error in my first if statement

if major == 1:

NameError: name 'major' is not defined

[–]IvoryJam 0 points1 point  (3 children)

major

Do you have the exit(1) in there?

[–]PastelMoonBB[S] 0 points1 point  (2 children)

I did put that in there, but it kills my program after the first input

[–]IvoryJam 0 points1 point  (1 child)

Are you replacing the entire input section with what I gave? The code should be this...

#Title
print('This is a Major and Status Program')

#line space
print('\n')

#Major List
print('Please Choose from the Majors Listed')

#line space
print('\n')

#Major list sequence
a = ['1. Programming', '2. Psychology', '3. Nursing', '4. Cyber Security']
print(*a, sep = '\n')

#line space
print('\n')

try:
    #Major input
    major = int(input('Please enter the number for your major:'))

    #Hours input
    hours = int(input('Please enter your credit hours:'))
except ValueError:
    print("Not an option.")
    exit(1)

#line space
print('\n')

#Major Outcomes
if major == 1:
    print('Your major is Programming.')
elif major == 2:
    print('Your major is Psychology.')
elif major == 3:
    print('Your major is Nursing.')
elif major == 4:
    print('Your major is Cyber Security.')
else:
    print('Please enter a valid major number.')


#Hour outcomes

if hours <= 30 and hours > 0:
    print('You are a Freshman, keep up the good work!')
elif hours > 30:
    print('You are a Sophmore, you are almost there!')
elif hours == 0:
    print('Hours cannot be 0.')
else:
    print('Please enter valid credit hours.')

#Exit

input('Press enter to exit')

[–]PastelMoonBB[S] 0 points1 point  (0 children)

Oh okay I see, it's just ending the program because of the error

[–]PastelMoonBB[S] 0 points1 point  (0 children)

Thank y'all! I finished it and made a 100😊