Question
Add a new column to table If you inspect the data files that we have provided, you will notice they all contain a sales section.
Add a new column to table
If you inspect the data files that we have provided, you will notice they all contain a sales section.
A sales section begins and ends with
For example:
2019-05-14,100,2 2018-12-03,201,3
In that sales section, album with ID 100 had 2 sales on 2019-05-15 and the album with ID 201 and 3 sales on 2018-12-03.
Give Music would like the summary of the albums to now include the number of sales of each album, as shown in the example table below.
To achieve this you should modify your program from the previous question to include a read_sales(lines, albums) function that is called after the albums have all been read by the extract_all_albums function. The read_sales function takes the following parameters:
- The list of lines read from the file
- The dictionary of albums returned by extract_all_albums.
The read_sales function should locate the sales section and for each sales record it should call the add_to_sold method of the appropriate album to update the number of albums sold.
Notes:
- You will also need to update your summary printing code - hopefully it is in a function which will make this easier.
- The table header line is now underlined by 45 '-' characters rather than 35 as before.
- The format string used for each albums row (and the table header) is now '{:<5}{:<30}{:>10}'
album0.txt
the previous code
"""program""" import os.path class Album: """Stores the information about an album""" def __init__(self, album_id, name, band, cost): """The data that forms an album, note that: - album_id is a int - cost is a float - the rest are strings """ self.album_id = album_id self.name = name self.band = band self.cost = cost self.total_sold = 0 def add_to_sold(self, count): """Adds to the internal album counter of the album""" if count >= 0: self.total_sold += count else: template = "Attempt to add {} to Album: {}. VALUE MUST NOT BE NEGATIVE" raise ValueError(template.format(count, self.album_id)) def __str__(self): output = "" output += "Album Name: {} ".format(self.name) output += "Band: {} ".format(self.band) output += "Purchase Price: ${:.2f}".format(self.cost) return output def __repr__(self): return "<<{}>>".format(self.album_id) def extract_album(lines, index): """Insert your function here""" identity = lines[index+1].strip() album = lines[index+2].strip() band = lines[index+3].strip() price = float(lines[index+4].strip()) return Album(identity, album, band, price) def extract_all_albums(lines): '''Function to extract all albums from the given list of lines''' i = 0 album_dict = {} while i < len(lines): if "
main()
For example:
Input | Result |
---|---|
album0.txt | Enter a data file name: album0.txt Album Catalogue Summary --------------------------------------------- ID ALBUM NAME # SOLD 70 Enter the Wu_Tang: 36 Cham... 23497 115 Daydream Nation 31088 323 Licensed to Ill 24755 414 52nd Street 65422 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started