Question
Download file rank.py (from Blackboard) and modify the body of the function determine_rank() so that it uses a dictionary to do the exact same mapping
Download file rank.py (from Blackboard) and modify the body of the function determine_rank() so that it uses a dictionary to do the exact same mapping that the if-elif-else structure does.
rank.py
def determine_rank(years):
if years == 1: return "Freshman" elif years == 2: return "Sophomore" elif years == 3: return "Junior" elif years == 4: return "Senior" else: return "Oops!Something is wrong with you! Hurry up!"
def main(): ## Determine an admission fee based on age group. print("Enter how many years you are attending BC", end="") years = eval(input("(only full numbers): ")) print("Then you are ...", determine_rank(years) )
main()
Run and test the program before you do any changes, so that you familiarize with it.
Note: we have done something very similar in class (look at the slides from last lecture)
Make sure your program doesnt crash if the user provides an input for which you do not have a mapping (similar to what the else-code does).
o Hint: use the get() built-in method (or the in/not in operators)
The updated program should be the running in the same way as the initial version.
Extrachallenge:Addvalidationinmain()fortheuserinput(usingtry...except).
********************************** SAMPLE OUTPUT ************************************
Enter how many years you are attending BC (only full numbers): 4 Then you are ... Senior
***************************************************************************************
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started