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()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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()
 
Expert Solution
steps

Step by step

Solved in 6 steps with 4 images

Blurred answer
Knowledge Booster
void method
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education