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
#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


Messages In This Thread
RE: don't know indenting of break and continue - by perfringo - Sep-18-2020, 09:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sublime Text + Anaconda not indenting Python code correctly Maxximiliann 0 2,014 Jun-13-2020, 06:09 PM
Last Post: Maxximiliann
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,999 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  IDLE indenting 29 columns after defining function Cosmo_Kane 1 2,437 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