Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
NBA Stats Application
The Helper Function
Core Functions
Menu Functions
Putting it all together
17. The do_action() function

Instruction

Very good! We now need something to invoke the appropriate function for the user's menu option choice.

Exercise

Create a function named do_action(players, user_choice). It should invoke a function based on user_choice, which will be a string.

Inside the function, you should first invoke the return_sorted_clubs(players) function and assign the result to a clubs variable. Then, for user_choice equal:

  • '1' – invoke show_sorted_players(players).
  • '2' – invoke show_top_player_in_club(players, clubs).
  • '3' – invoke show_player_club_relation(players, clubs).
  • '4' – invoke show_player_most_clubs(players).

For all other input, print 'Incorrect choice!'

In the previous exercises, we assumed that user always provides correct output. Now that we use those functions in a do_action() function, we should provide that user doesn't get an error when choosing incorrect option somewhere in the application – incorrect number (e.g., 58) or a string which can't be converted to an integer (e.g., 'answer 1'). When a user types an incorrect answer, our function should always print 'Incorrect choice!'.

Stuck? Here's a hint!

Remember to create a clubs variable inside a function (invoke a return_sorted_clubs(players) function).

Then, use a try-except syntax. Inside try, use the if ... elif ... else construction.