93.7k views
1 vote
Lyrics = ["I wanna be your endgame", "I wanna be your first string",

"I wanna be your A-Team", "I wanna be your endgame, endgame"]
lines_of_sanity = 6

#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.

#Imagine you have a song stuck in your head. Worse, you have
#only a few lines from a song stuck in your head. They just
#keep repeating over and over. The specific lines you have
#stuck in your head are stored in the variable lyrics.
#
#You can only stay sane so long while doing this.
#Specifically, you can only listen to lines_of_sanity lines
#before going crazy. Once you've reached lines_of_sanity,
#your brain will finish out the current list, then crumble.
#
#Write some code that will print the lyrics that run through
#your head. It should keep repeating each line one-by-one
#until you've reached lines_of_sanity lines. Then, it should
#keep going to finish out the current verse. After that, print
#"MAKE IT STOP" in all caps (without the quotes).
#
#HINT: Remember, we can iterate through items in a list using
#this syntax:
#
# for item in list_of_items:
#
#HINT 2: You'll probably need a counter to count how many lines
#have been printed so far.

#This
#should print 9 lines: all 4 lines of the list twice, followed
#by MAKE IT STOP

User Vinayrks
by
4.7k points

1 Answer

4 votes

Answer:

I am writing the Python program:

lyrics = ["I wanna be your endgame", "I wanna be your first string",

"I wanna be your A-Team", "I wanna be your endgame, endgame"]

lines_of_sanity = 6

counter = 0

while counter <= lines_of_sanity+1:

for line in lyrics:

print(line)

counter = counter + 1

print("MAKE IT STOP")

Step-by-step explanation:

  • The lyrics is a list which has the following lines of song.

I wanna be your endgame

I wanna be your first string

I wanna be your A-Team

I wanna be your endgame, endgame

  • The value of lines_of_sanity is 6.
  • counter variable is initialized to 0.
  • The while loop starts which keeps executing until the value of counter becomes greater than 6 which is the value of lines_of_sanity
  • The for loop inside the while loop moves through each line of lyrics list and prints that line. The counter variable increments to 1 at each iteration.
  • Lets see what the while and for loops print at each iteration:

1st iteration: counter = 0, following line is displayed:

  • I wanna be your endgame
  • Value of counter increments by 1.
  • counter = 1

2nd iteration: counter = 1, following line is displayed:

  • I wanna be your first string
  • Value of counter increments by 1.
  • counter = 2

3rd iteration: counter = 2, following line is displayed:

  • I wanna be your A-Team
  • Value of counter increments by 1.
  • counter = 3

4th iteration: counter = 3, following line is displayed:

  • I wanna be your endgame, endgame
  • Value of counter increments by 1.
  • counter = 4

5th iteration: counter = 4, following line is displayed:

  • I wanna be your endgame
  • Value of counter increments by 1.
  • counter = 5

6th iteration: counter = 5, following line is displayed:

  • I wanna be your first string
  • Value of counter increments by 1.
  • counter = 6

7th iteration: counter = 6, following line is displayed:

  • I wanna be your A-Team
  • Value of counter increments by 1.
  • counter = 7
  • The program should keep going to finish out the current verse. So following line is displayed:
  • I wanna be your endgame, endgame
  • The loop breaks as the value of counter = 7 is greater than lines_of_sanity i.e. 6
  • So at last the print("MAKE IT STOP") statement is execute which displays the following line:
  • MAKE IT STOP
  • The program along with the output is attached.
Lyrics = ["I wanna be your endgame", "I wanna be your first string-example-1
User Thomaskeefe
by
5.3k points