Python Forum
don't know indenting of break and continue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
don't know indenting of break and continue
#1
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!")
Reply
#2
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
Reply
#3
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
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.
Reply
#5
mlieqo -- thank you so much. That was all I needed! Figured it was something obvious I was missing.
Reply
#6
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')
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sublime Text + Anaconda not indenting Python code correctly Maxximiliann 0 1,985 Jun-13-2020, 06:09 PM
Last Post: Maxximiliann
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,972 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  IDLE indenting 29 columns after defining function Cosmo_Kane 1 2,424 Jun-03-2018, 08:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020