Language: Python 3.6 Function name: buy album:s Parameters; a dictionary named a
ID: 3598483 • Letter: L
Question
Language: Python 3.6
Function name: buy album:s Parameters; a dictionary named album_buy_dict (key is the name of an album (string): value is a the album's artist (string)), a dictionary named artist_price_dict (key is name of an artist (string): value is the price per album of all that artist's albums (float) Returns; a dictionary (key is the name of an artist (string): value is how much was paid in total for all of the albums we bought that were theirs (float) Description: Write a function that takes in a dictionary in which every key is the name of an album, and every is a string of the name of the album's artist, as well as a dictionary in which every key is an artist's name, and every value is a float of the price per album for that artist for all their albums. The function should return a dictionary in which every key is the name of an artist whose album(s) you bought, and the value is the total amount you paid for the album(s) of that particular artist (i.e. price per that artist's albums total number of albums bought). The dictionary should also have a key-value pair where the key is "total" and the value is the grand total for all of the albums you bought. Authors: Christine Feng and Rodrigo Meja CS1301 -HOMEWORK 07: INTRO TO DICTIONARIES Notes: You can assume spelling and punctuation of the artist names in album_buy_dict and artist_price_dict will be the same for the same artist. Each album included in album_buy_dict means we bought one of that album. You can assume that "total" will never be a key in the original dictionary If you print out your returned dictionary the order of the artists may be different from ours, what's important is that each artist (key) is correctly matched to their price totals value . . . ases >>> total-buy-dict = buy-albums ({"Coloring Book" : "Chance the Rapper", "Acid Rap": "Chance the Rapper", "The College Dropout": "Kanye West", "Yeezus" "Kanye West", "My Beautiful Dark Twisted Fantasy" "Kanye West", "Channel Orange" "Frank Ocean", "Blonde" "Frank Ocean", "Kauai": "Childish Gambino", "Continuum" : "John Mayer''), {"Chance the Rapper" · e.ee, "Kanye West" 9.75, "Frank Ocean": 12.15, "Childish Gambino" : 8.45, "John Mayer" : 4.75]) >>>print (total _buy_dict) >Chance the Rapper 0.0, Kanye West': 29.25, Frank Ocean': 24.30, Childish Gambino': 8.45, 'John Mayer: 4.75 'total': 66.75]Explanation / Answer
def buy_albums(album_buy_dict, artist_price_dict):
total_buy_dict = {}
for album in album_buy_dict:
if album_buy_dict[album] in total_buy_dict:
total_buy_dict[album_buy_dict[album]] += 1
else:
total_buy_dict[album_buy_dict[album]] = 1
total = 0
for artist in total_buy_dict:
cost = total_buy_dict[artist]*artist_price_dict[artist]
total += cost
total_buy_dict[artist] = cost
total_buy_dict['total'] = total
return total_buy_dict
print(buy_albums({"Coloring Book": "Chance the Rapper", "Acid Rap": "Chance the Rapper", "The College DropOut" : "Kanye West", "Yeezus" : "Kanye West", "My Beautiful Dark Twisted fantasy": "Kanye West", "Channel Orange" : "Frank Ocean", "Blonde" : "Frank Ocean", "Kauai": "Childish Gambino", "Continuum": "John Mayer"}, {"Chance the Rapper": 0.00, "Kanye West": 9.75, "Frank Ocean": 12.15, "Childish Gambino": 8.45, "John Mayer": 4.75}))
# copy pastable code link: https://paste.ee/p/FihjE
'''
Sample run
'''
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.