Question

In: Computer Science

9) This is in Python Code a program that allows the user to manage its expenses...

9) This is in Python

Code a program that allows the user to manage its expenses accepting the following commands:

- Add <player_name> -> This command adds a new player to the system. Assume there can’t be repeated names. If a name already exists then an error is shown to the user.

- Score <player_name> <score> -> This command adds the score to the player with the name player-name. Each score needs to be added individually

- Report -> This commands prints the name of each player and the top three scores they have. If they have less than three scores then it prints all of the scores for that player. After printing each name and the top three scores for each player it prints the total score for all players

-Exit -> Stops the program

Solutions

Expert Solution

Python Program:

""" Python program that executes based on commands """

# Initializing players dictionary
players = {};

# Loop user want to exit
while True:
   # Reading commands
   command = input("Enter your Command: ")
  
   # Splitting on space
   tokens = command.strip().split()
  
   # Fetching first word of command
   cmd = tokens[0]
  
   # Checking command
   if cmd.lower() == 'add':
       # Reading name
       name = tokens[1]
      
       # Checking for existence
       if name in players.keys():
           print("\nPlayer ", name, "already exists...\n")
       else:
           # Adding player
           players[name] = []
          
   # Command is Score
   elif cmd.lower() == 'score':
       # Reading name
       name = tokens[1]
      
       # Checking for existence
       if name not in players.keys():
           print("\nPlayer ", name, "doesn't exist...\n")
       else:
           # Adding score
           players[name].append(float(tokens[2]))
          
   # Command is Report
   elif cmd.lower() == 'report':
       # Printing player name and score
       for player in players.keys():
           # Sorting scores
           players[player].sort(reverse=True)
           # Printing data
           print("\nPlayer: ", player, end = "\t")
           # Printing scores
           if len(players[player]) >= 3:
               print("Scores: ", players[player][0], players[player][1], players[player][2])
           else:
               print("Scores: ",end="")
               for score in players[player]:
                   print(score, end=" ")
                  
       # Total score of all players
       total = 0
       for player in players.keys():
           for score in players[player]:
               total = total + score
              
       # Printing total score
       print("\n\nTotal Score: ", total, "\n\n")
  
   # Command is Exit
   elif cmd.lower() == 'exit':
       print("Bye!")
       break
      
   # any other command
   else:
       print("Invalid Command")

_______________________________________________________________________________________________________________________

Code Screenshot:

_______________________________________________________________________________________________________________________

Sample Run:


Related Solutions

Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
Use Visual Python or equivalent, to write a program that allows the user to observe the...
Use Visual Python or equivalent, to write a program that allows the user to observe the following: Damped and forced harmonic oscillators. The program should be user friendly and have default values for the initial velocities, positions, masses, and spring constants as well as damping constants. Values and frequencies for the forced oscillators should also be given. It should allow the user to input values for the velocities, positions, spring constants, and masses. The program should determine automatically the scale...
Write in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
Create a program in visual basic that allows the user to manage vehicle information including: •...
Create a program in visual basic that allows the user to manage vehicle information including: • License plate number • Owner name • Owner phone number The user interface will be GUI-based providing the following capabilities: • Add up to 100 vehicles • Remove specified vehicles • View a sorted list of all known license plates • View the data for a specified vehicle • Save the database contents into a file • Load a saved database from a file
Python Add a command to this chapter’s case study program that allows the user to view...
Python Add a command to this chapter’s case study program that allows the user to view the contents of a file in the current working directory. When the command is selected, the program should display a list of filenames and a prompt for the name of the file to be viewed. Be sure to include error recovery in the program. If the user enters a filename that does not exist they should be prompted to enter a filename that does...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a...
USING PYTHON. Thank you in advance Write a program that allows the user to enter a series of string values into a list. When the user enters the string ‘done’, stop prompting for values. Once the user is done entering strings, create a new list containing a palindrome by combining the original list with the content of the original list in a reversed order. Sample interaction: Enter string: My Enter string: name Enter string: is Enter string: Sue Enter string:...
I am creating a program in Python that allows the user to input a credit card...
I am creating a program in Python that allows the user to input a credit card number, and determine if the card is valid. This should be done by taking every other number, starting from the right, and adding them together. The doubling each of the other digits, and adding them together as single digits, and then adding the two sums together. For example, if the number 4833 1200 3412 3456 is used, the first sum should be 6+4+2+4+0+2+3+8=29, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT