EmKhay
EmKhay

Reputation: 37

How to write a while loop inside of a function with python?

I am going through the book 'Python Crash Course' and the problem is as follows:

8-8. User Albums: Start with your program from Exercise 8-7 . Write a while loop that allows users to enter an album’s artist and title . Once you have that information, call make_album() with the user’s input and print the dictionary that’s created . Be sure to include a quit value in the while loop .

I did 8-7 no problem, but trying to add the functionality, the loop to solve 8-7 I can't figure out.

Here was the code I already tried:

    while True: 
        print('Give me an artist.')
        artist=input()
        print('Give me an album.')
        album=input()

        if artist == 'quit':
            break
        elif track_no:
            track_no=input()
            albums = {'Artist': artist, 'Album':album, 'Track Number':track_no}
        else:
            albums = {'Artist': artist, 'Album':album}

    print(albums)



Here is my code from the 8-7 problem:

def make_album(artist, album, track_no=' '):
    if track_no:
        albums = {'Artist': artist, 'Album':album, 'Track Number':track_no}
    else:
        albums = {'Artist': artist, 'Album':album}

    print(albums)

Upvotes: 0

Views: 246

Answers (3)

ParthS007
ParthS007

Reputation: 2691

Pass the Input directly to function then print the albums dict in the function.

while True:

    print('Give me an artist.')

    artist=input()
    if artist == 'quit':
        break

    print('Give me an album.')
    album=input()

    print('Give the Track No.')
    track_no=input()

    make_album(artist, album, track_no)

Add the quit condition just after the requesting the artist input otherwise, it will move on to the next input.

Upvotes: 1

Iiskaandar
Iiskaandar

Reputation: 394

while True: 
    print('Give me an artist.')
    artist=input()
    print('Give me an album.')
    album=input()

    if artist == 'quit':
        print ('Artist': artist, 'Album':album)
        break

Upvotes: 1

John Gordon
John Gordon

Reputation: 33275

I think this is what you're looking for:

while True: 
    print('Give me an artist.')
    artist=input()
    print('Give me an album.')
    album=input()

    if artist == 'quit':
        break

    make_album(artist, album)

Upvotes: 1

Related Questions