roninbeast
roninbeast

Reputation: 15

Problem with the Python code and i can't figure where

This is how my code looks, the problem is that this program i mean my code is not saving the user input in user_info.json file. The program on itself without saving works correctly but whenever it comes to saving the file it doesn't. If this description or code is chaotic, etc. just let me know and I'll edit it. import json

def make_album(album_title, artist, songs=None):
    """Shows info about artist and an album."""
    info = {'album': album_title.title(),
        'artist': artist.title(),
        }       
    if songs:
        info['songs'] = songs
    return info 

title_prompt = "\nWhat's the name of the album? "
artist_prompt = "Who is the artist? "
song_prompt = "If you know the number of songs on album, write it down,"
song_prompt += " otherwise press 'enter'. "


print("(if you want to shut the program, press 'q')")

def input_album():
    """Accepts user data."""
    while True:
        album = input(title_prompt)
        if album == 'q':
             break

        art = input(artist_prompt)
        if art == 'q':
             break

        song = input(song_prompt)
        if song == 'q':
             break
   
        whole_info = make_album(album, art, song)
    
def save_album():
    """Saves user info in user_info.json."""
    user_info = input_album()
    filename = 'user_info.json'

    with open(filename, 'w') as f:
        json.dump(user_info, f)
    return user_info

Upvotes: 0

Views: 69

Answers (1)

kakou
kakou

Reputation: 682

Your issue is, as described in the comment of Scratch'N'Purr that you do not return anything from input_album()

Although assuming you return the whole_info or even the invocation directly:

whole_info = make_album(album, art, song)
return whole_info

or

return make_album(album, art, song)

That means that your program will exit after the first iteration. Maybe what you need is to set up an empty list before iteration, and add to this list all the album info inputted from your user and in the end return it.So something like this:

def input_album():
    """Accepts user data."""
    albums = []
    while True:
        album = input(title_prompt)
        if album == 'q':
             break

        art = input(artist_prompt)
        if art == 'q':
             break

        song = input(song_prompt)
        if song == 'q':
             break
   
        new_album = make_album(album, art, song)
        albums.append(new_album)
    return albums

Upvotes: 1

Related Questions