The coding language is python. I need to use one function to print a menu and then functions for each item in the menu. The code I wrote before using functions is below. I need help using functions to create the same output. Each menu item needs its own function and another function for the whole menu. Please help! choice = ' 'while choice != "6":print("Select an option: \n 1. Add a player \n 2. Remove a player \n 3. Change a player's rating \n 4. Output players above a certain rating \n 5. Output the roster \n 6. Exit")choice = input("Option: ") #selects which option for menu to run if choice == "1":new_jersey = int(input("Enter a new jersey number: ")) #gets new, unique player numbernew_rating = int(input("Enter the corresponding rating: ")) #gives the jersey number a corresponding ratingwhile (new_jersey < 0) or (new_jersey > 99): print('Out of range')new_jersey = int(input("Enter a new jersey number: ")) #gets new, unique player numberwhile (new_rating < 1) or (new_rating > 9):print('Out of range')new_rating = int(input("Enter the corresponding rating: ")) #gives the jersey number a corresponding ratingif new_jersey in roster: #find duplicates in rosterprint('Already in roster')new_jersey = int(input("Enter a new jersey number: ")) #prompts user for new jersey number if already in roster else:print('Not in roster') roster[new_jersey] = new_rating #adds player to dictionary if player not already in there print(roster)if choice == "2":jersey_num = int(input("Enter a jersey number: ")) #selects a jersey number from userif jersey_num in roster.keys(): #find duplicates in rosterprint('In roster') else:while jersey_num not in roster.keys():print('Not in roster')jersey_num = int(input("Enter a new jersey number: ")) #prompts user for new jersey number if already in roster del roster[jersey_num] #deletes that jersey number from the dictionaryprint(roster) #prints the updated rosterif choice == '3':#prompt for the jersey numberjersey_number = int(input('Enter a jersey number: '))if jersey_number in roster.keys():#prompt for the rating of the playerrating = int(input('Enter a new rating for player:\n'))while (rating < 1) or (rating > 9):print('Out of range')rating = int(input("Enter the corresponding rating: ")) #gives the jersey number a corresponding rating#update the ratingroster[jersey_number] = ratingprint(roster)else:print('Not in roster')jersey_number = int(input('Enter a jersey number: '))if choice == '4':val = int(input("Enter rating to be compared to (1-9): ")) #gets value to compare ratings to while val > 9 or val < 0:print ('Out of range')val = int(input("Enter the new rating between 1 and 9: ")) #prompts user for new ratingelse: print('In range')output = dict((k,i) for k, i in roster.items() if i > val) #looks at key-value pairings in roster and evaluates if ratings (i) is above a certain rating then stores it into dictionary outputprint(output) #prints the key-value pairings found in line 51if choice == '5':print(roster) #prints rosterif choice == '6':quit #stops the menu from running
The coding language is python.
I need to use one function to print a menu and then functions for each item in the menu. The code I wrote before using functions is below. I need help using functions to create the same output.
Each menu item needs its own function and another function for the whole menu. Please help!
choice = ' '
while choice != "6":
print("Select an option: \n 1. Add a player \n 2. Remove a player \n 3. Change a player's rating \n 4. Output players above a certain rating \n 5. Output the roster \n 6. Exit")
choice = input("Option: ") #selects which option for menu to run
if choice == "1":
new_jersey = int(input("Enter a new jersey number: ")) #gets new, unique player number
new_rating = int(input("Enter the corresponding rating: ")) #gives the jersey number a corresponding rating
while (new_jersey < 0) or (new_jersey > 99):
print('Out of range')
new_jersey = int(input("Enter a new jersey number: ")) #gets new, unique player number
while (new_rating < 1) or (new_rating > 9):
print('Out of range')
new_rating = int(input("Enter the corresponding rating: ")) #gives the jersey number a corresponding rating
if new_jersey in roster: #find duplicates in roster
print('Already in roster')
new_jersey = int(input("Enter a new jersey number: ")) #prompts user for new jersey number if already in roster
else:
print('Not in roster')
roster[new_jersey] = new_rating #adds player to dictionary if player not already in there
print(roster)
if choice == "2":
jersey_num = int(input("Enter a jersey number: ")) #selects a jersey number from user
if jersey_num in roster.keys(): #find duplicates in roster
print('In roster')
else:
while jersey_num not in roster.keys():
print('Not in roster')
jersey_num = int(input("Enter a new jersey number: ")) #prompts user for new jersey number if already in roster
del roster[jersey_num] #deletes that jersey number from the dictionary
print(roster) #prints the updated roster
if choice == '3':
#prompt for the jersey number
jersey_number = int(input('Enter a jersey number: '))
if jersey_number in roster.keys():
#prompt for the rating of the player
rating = int(input('Enter a new rating for player:\n'))
while (rating < 1) or (rating > 9):
print('Out of range')
rating = int(input("Enter the corresponding rating: ")) #gives the jersey number a corresponding rating
#update the rating
roster[jersey_number] = rating
print(roster)
else:
print('Not in roster')
jersey_number = int(input('Enter a jersey number: '))
if choice == '4':
val = int(input("Enter rating to be compared to (1-9): ")) #gets value to compare ratings to
while val > 9 or val < 0:
print ('Out of range')
val = int(input("Enter the new rating between 1 and 9: ")) #prompts user for new rating
else:
print('In range')
output = dict((k,i) for k, i in roster.items() if i > val) #looks at key-value pairings in roster and evaluates if ratings (i) is above a certain rating then stores it into dictionary output
print(output) #prints the key-value pairings found in line 51
if choice == '5':
print(roster) #prints roster
if choice == '6':
quit #stops the menu from running
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 7 images