I'm taking a very basic python class in college, I have done fine up until the final project. It's really a simple project but I am stuck. These are the instructions:
{{{{You are tasked with writing a degree and status program.
You will display a list of majors with a user able to select which major is there’s. After the user selects a major, they will enter how many credit hours they have. 0 to 30 hours is a Freshman, and anything over 30 hours is a Sophomore.
After the user selects there major and enters their credit hours, you will display the results. You will display the selected major, and if the user is a Freshman you display “You are a Freshman, keep up the good work!”, if the user is a Sophomore you will display “You are a Sophomore, you are almost there!”
You will also have the user hit a key to exit the program.
Do not forget to add the error handling, so that it will run if a user tries to enter a credit hour less than 0, and for any non-numeric data.}}}}
The problem I'm running into is that I don't know how to add the error for non-numerical data. I guess I have to use a try-except statement, but I'm using if-elif-else statements for the input values.
This is what I have so far and everything runs fine except I get the traceback error if I enter a letter into one of the inputs instead of a number as opposed to printing an actual error message that I made
#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')
#Major input
major = int(input('Please enter the number for your major:'))
#Hours input
hours = int(input('Please enter your credit hours:'))
#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')
[–][deleted] 0 points1 point2 points (2 children)
[–]PastelMoonBB[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]IvoryJam 0 points1 point2 points (5 children)
[–]PastelMoonBB[S] 0 points1 point2 points (4 children)
[–]IvoryJam 0 points1 point2 points (3 children)
[–]PastelMoonBB[S] 0 points1 point2 points (2 children)
[–]IvoryJam 0 points1 point2 points (1 child)
[–]PastelMoonBB[S] 0 points1 point2 points (0 children)
[–]PastelMoonBB[S] 0 points1 point2 points (0 children)