OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Can't break a while loop with user input (Python)

  • Thread starter Thread starter Dumby
  • Start date Start date
D

Dumby

Guest
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 arguments in the function. But if input at any moment is == 'q', the loop should be terminated. The function is following:

Code:
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:

Code:
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:

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)

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

The result is: Enter album's name:

2)

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

Code:
    if artist == 'q':
        create_album = False

Code:
    if artist == 'Q':
        create_album = False

The result is: Enter album's name:
<p>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 arguments in the function. But if input at any moment is == 'q', the loop should be terminated.
The function is following:</p>
<pre><code>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
</code></pre>
<p>It is placed inside the while loop:</p>
<pre><code>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))
</code></pre>
<p>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:</p>
<pre><code>number_of_tracks = input("(Optional) Enter number of tracks: ")
if number_of_tracks =='q'.lower():
create_album = False
</code></pre>
<p>How do I fix the program, so it terminates the while loop in any moment if 'q' is entered by the user?</p>
<p>I've tried to change various ways to solve the issue.</p>
<p>1)</p>
<pre><code> if artist == 'q'.lower():
break
</code></pre>
<p>The result is:
<code>Enter album's name: </code></p>
<p>2)</p>
<pre><code> if artist == 'q'.title():
create_album = False
</code></pre>
<pre><code> if artist == 'q':
create_album = False
</code></pre>
<pre><code> if artist == 'Q':
create_album = False
</code></pre>
<p>The result is:
<code>Enter album's name: </code></p>
 

Latest posts

Top