Search code examples
pythonfunctionwhile-loopbreak

I can't break a while loop with user input


I'm learning to use functions in while loops. My goal is to ask the user for input inside the while loop and use the input as an argument in the function. But if input at any moment is == 'q', the loop should be terminated.

The function is following:

def make_album(artist_name, album_name, tracks = None):
    if tracks:
        album = {
            'artist': artist_name,
            'album': album_name,
            'number of tracks': tracks
        }
        return album
    else:
        album = {
            'artist': artist_name,
            'album': album_name,
            
        }
        return album

It is placed inside the while loop:

create_album = True
while create_album:
    
    artist = input("Enter atrist's name: ").title()
    if artist == 'q'.lower():
        create_album = False
        
    album = input("Enter album's name: ").capitalize()
    if album == 'q'.lower():
        create_album = False
        
    number_of_tracks = input("(Optional) Enter number of tracks: ")
    if number_of_tracks =='q'.lower():
        create_album = False
    elif number_of_tracks:
        print(make_album(artist, album, number_of_tracks))
    else:
        print(make_album(artist, album))

Despite the fact that every input is checked with if statement and should change the value of create_album to False if input is 'q', it only works properly in the last part of the code:

number_of_tracks = input("(Optional) Enter number of tracks: ")
    if number_of_tracks =='q'.lower():
        create_album = False

How do I fix the program, so it terminates the while loop in any moment if 'q' is entered by the user?

I've tried to change various ways to solve the issue.

1)

    if artist == 'q'.lower():
        break

The result is: Enter album's name:

2)

    if artist == 'q'.title():
        create_album = False
    if artist == 'q':
        create_album = False
    if artist == 'Q':
        create_album = False

The result is:

Enter album's name:


Solution

  • Once you identify that the user has entered "q", you set create_album to False, but then you fall through to the next input.

    You could add a break, or in this case even a continue, to the if bodies of the tests for 'q'. Also, artist == 'q'.lower() doesn't make sense, since 'q' is already lowercase. Instead, you want artist.lower() == 'q':

        if artist.lower() == 'q':
            create_album = False
            break
    

    But for this code it really doesn't make sense to have create_album at all. Instead, you can replace the entire thing with:

    while True:   
        artist = input("Enter atrist's name: ").title()
        if artist.lower() == 'q':
            break
            
        album = input("Enter album's name: ").capitalize()
        if album.lower() == 'q':
            break
            
        number_of_tracks = input("(Optional) Enter number of tracks: ")
        if number_of_tracks.lolwer()=='q':
            break
        elif number_of_tracks:
            print(make_album(artist, album, number_of_tracks))
        else:
            print(make_album(artist, album))