I
I
idr19952020-07-27 21:26:02
Python
idr1995, 2020-07-27 21:26:02

How to store data in a python dictionary?

Hello!
I'm doing the next activity:

User Albums: Start with your program from Exercise 8-7. Write a while loop that allows users to enter an artist name and an album name. Once you have this information, call make_album() with user input and print the generated dictionary. Don't forget to include the exit value in the while loop.

my code:

active = True
while active:
    album = {}

    name = input()
    track_name = input()
    album[name] = track_name

    if name == 'q':
        break

    def make_album(album):
        return album

    print(make_album(album))


code from exercise 8.7
def make_album(group, name_track, count_tr=None):

    if count_tr:
        album = {}
        album['group'] = group
        album['name_track'] = name_track
        album['count'] = count_tr

        return album
    else:
        album = {}
        album['group'] = group
        album['name_track'] = name_track
        return album

print(make_album('scriptonit', 'animals', 24))
print(make_album('limp bizkit', 'golden cobra'))
print(make_album('max korzh', 'stilevo', 12))


how to store data in three dictionaries dictionary_1
= {name, track}
dictionary_2 = {name, track}
dictionary_3 = {name, track}???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Trainin, 2020-07-27
@Stormx480

It's not entirely clear what you want.
If you want to save the result of the make_album function (i.e. save the dictionary that this function returns), then simply write the function call to a variable. Thus, you will store the result of the function in this variable.

album_1 = make_album('limp bizkit', 'golden cobra')
album_2 = make_album('limp bizkit', 'take a look around')

And you will write new dictionaries, because each time in the make_album function you create a new local variable album, which you later return.
If I misunderstood you, clarify the question, and I will help you with the answer.

P
PavelMos, 2020-07-31
@PavelMos

Storing data in a dictionary means
1) creating a new dictionary element:
slovar={}
slovar[a]='b'
or
2) modifying an existing element
slovar={1:'a',2:'b'}
slovar[1]= 'z'
in the example in the question is just a function that creates and returns a dictionary

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question