For this task, you are to write code that tracks how much money is spent on various leisure activities. Instructions You like to go out and have a good time on the weekend, but it's really starting to take a toll on your wallet! To help you keep a track of your expenses, you've decided to write a little helper program. Your program should be capable of recording leisure activities and how much money is spent on each. You are to add the missing methods to the LeisureTracker class as described below. a) The add_activity method This method takes the activity name and the cost of an activity, and adds it to the total cost for that activity. The total costs are to be recorded in the activities instance variable, which references a dictionary object. You will need to consider two cases when this method is called: No costs have been recorded for the activity yet (i.e. the activity name is not in the dictionary). The activity already has previous costs recorded (i.e. the activity name is already in the dictionary with an associated total cost). b) The print_summary method This method takes no arguments, and prints the name and total cost of each activity (the output can be in any order, so no sorting required). Additionally, you are to display the total cost of all activities and the name of the most expensive activity. Costs are to be displayed with two decimal places of precision. You can assume that add_activity has been called at least once before print_summary (that is, you don't need to worry about the leisure tracker not containing any activities). Hint: If you don't remember how to iterate over the items in a dictionary, you may wish to revise Topic 7. Requirements To achieve full marks for this task, you must follow the instructions above when writing your solution. Additionally, your solution must adhere to the following requirements: You must use f-strings to format the outputs (do not use string concatenation). You must ensure that the costs are printed with two decimal places of precision. You must only use the activities instance variable to accumulate and store activity costs. You must use a single loop to print individual activity costs and aggregate both the total cost and most expensive activity (do not use Python functions like sum or max). You must not do any sorting. Example Runs Run 1 Cinema: $48.50 Mini golf: $125.98 Concert: $90.85 TOTAL: $265.33 MOST EXPENSIVE: Mini golf Your code should execute as closely as possible to the example runs above. To check for correctness, ensure that your program gives the same outputs as in the examples, as well as trying it with other inputs. class LeisureTracker: def__init__(self): self.activities = {} # Write your methods here tracker = LeisureTracker() tracker.add_activity('Cinema', 23.50) tracker.add_activity('Mini golf', 125.98) tracker.add_activity('Concert', 57.85) tracker.add_activity('Concert', 33.00) tracker.add_activity('Cinema', 25.00) tracker.print_summary()
For this task, you are to write code that tracks how much money is spent on various leisure activities.
Instructions
You like to go out and have a good time on the weekend, but it's really starting to take a toll on your wallet! To help you keep a track of your expenses, you've decided to write a little helper
Your program should be capable of recording leisure activities and how much money is spent on each. You are to add the missing methods to the LeisureTracker class as described below.
a) The add_activity method
This method takes the activity name and the cost of an activity, and adds it to the total cost for that activity. The total costs are to be recorded in the activities instance variable, which references a dictionary object. You will need to consider two cases when this method is called:
- No costs have been recorded for the activity yet (i.e. the activity name is not in the dictionary).
- The activity already has previous costs recorded (i.e. the activity name is already in the dictionary with an associated total cost).
b) The print_summary method
This method takes no arguments, and prints the name and total cost of each activity (the output can be in any order, so no sorting required). Additionally, you are to display the total cost of all activities and the name of the most expensive activity. Costs are to be displayed with two decimal places of precision.
You can assume that add_activity has been called at least once before print_summary (that is, you don't need to worry about the leisure tracker not containing any activities).
Hint: If you don't remember how to iterate over the items in a dictionary, you may wish to revise Topic 7.
Requirements
To achieve full marks for this task, you must follow the instructions above when writing your solution. Additionally, your solution must adhere to the following requirements:
- You must use f-strings to format the outputs (do not use string concatenation).
- You must ensure that the costs are printed with two decimal places of precision.
- You must only use the activities instance variable to accumulate and store activity costs.
- You must use a single loop to print individual activity costs and aggregate both the total cost and most expensive activity (do not use Python functions like sum or max).
- You must not do any sorting.
Example Runs
Run 1
Cinema: $48.50Mini golf: $125.98
Concert: $90.85
TOTAL: $265.33
MOST EXPENSIVE: Mini golf
Your code should execute as closely as possible to the example runs above. To check for correctness, ensure that your program gives the same outputs as in the examples, as well as trying it with other inputs.
Step by step
Solved in 6 steps with 4 images