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: