Python Forum

Full Version: don't know indenting of break and continue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was trying to get this output...

My Seven Favorite Musical Artists

Enter Musical Artist: Drake
Enter Musical Artist: Cardi B
Enter Musical Artist: Justin Bieber
No Way!
Enter Musical Artist: Ariana Grande
Enter Musical Artist: Travis Scott
Enter Musical Artist:
That's less than seven, but OK

1. Drake
2. Cardi B
4. Ariana Grande
5. Travis Scott

End of Favorites!

I wrote this code and definitely understand why it has errors, I just don't have ideas on how to fix them because I don't really know how and where to place "breaks" and "continues".

print("My Seven Favorite Musical Artists\n")

output = ""

for ii in range(1, 8):
    artist = input("Enter Musical Artist: ")
    output = output + str(ii) + ". " + artist + "\n"

    if artist.lower() == "justin bieber":
        break
    
        print("No way!")
        continue

print("\n" + output)

print("End of Favorites!")
break statement will exit the for loop so in your case
    if artist.lower() == "justin bieber":
        break
     
        print("No way!")   -> this line will never be executed as you exit the loop with the break statement above
        continue           -> also unreachable
in your case you don't want to use break at all and then only move the generating of the output after your if statement:

print("My Seven Favorite Musical Artists\n")
 
output = ""
 
for ii in range(1, 8):
    artist = input("Enter Musical Artist: ")
 
    if artist.lower() == "justin bieber": 
        print("No way!")
        continue

    output = output + str(ii) + ". " + artist + "\n"

print("\n" + output)
 
print("End of Favorites!")
this way in case of "justin bieber", continue statement will skip to next iteration of the loop and will not append the artist name in the output
This does what it is programmed to do.
What we don't know is what you want to happen when justin b. shows up.
He is already in your output string, when you test for it.
Paul
And also you will have to add another if to test for an empty string (""). In that case you must print "That's less than seven, but OK" and do a break to exit the for loop.
mlieqo -- thank you so much. That was all I needed! Figured it was something obvious I was missing.
It is unclear what should happen with Justin Bieber (should he be excluded from printing out) and should enumeration be continous or not (in example there are 1, 2, 4, 5)

Following approach assumes that Justin Bieber must be included and only message should be displayed.

Start with defining what should happen in spoken language:

Output:
repeat 7 times: if something is anwered: if answer is justin bieber then express disdain store answer somewhere else (no answer): print message that there is less than 7 break
You can get to else branch only if there are less than 7 answers (if there are 7 answers for-loop will stop itself with no possibility to end in else branch).

In 3.8 <= Python it could look like:

artists = list()

for i in range(7):                                      # repeat seven times
    if (artist := input('Enter Musical Artist: ')):     # if there is an answer (truthy, i.e. not empty)
        if artist.lower() == 'justin bieber':           # if the answer is justin bieber
            print('No way!')        
        artists.append(artist)                          # append artist to list (even Justin Bieber) 
    else:                                               # nothing entered
        print("That's less than seven but it's ok.")
        break


print(*(f'{num}. {artist}' for num, artist in enumerate(artists, start=1)), sep='\n')