Reputation: 37
I am trying to run a loop that prints lines of lyrics in a list, then stops when it hits the variable limit amount. Though i've tried it two ways with two different outcomes, neither are correct.
The problem I was given:
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.
#Recall the Earworm problem (3.3.5 Coding Exercise 2). The
#first time, you would still finish printing the entire list
#of lyrics after lines_of_sanity was exceeded.
#
#Revise that code so that you always stop when lines_of_sanity
#is reached. If lines_of_sanity is 6, you would print 6 lines,
#no matter how many lines are in the list. If there are fewer
#than 6 lines in the list, then you'd repeat the list until
#the number of lines is reached.
#
#For example, with the values above, you'd print:
#I wanna be your endgame
#I wanna be your first string
#I wanna be your A-Team
#I wanna be your endgame, endgame
#I wanna be your endgame
#I wanna be your first string
#MAKE IT STOP
#
#That's 6 lines: the entire list once, then the first two lines
#again to reach 6. As before, print MAKE IT STOP when you're
#done.
#
#HINT: There are multiple ways to do this: some involve a small
#change to our earlier answer, others involve a more wholesale
#rewrite. If you're stuck on one, try to think of a totally
#different way!
#Add your code here! Using the initial inputs from above, this
#should print 7 lines: all 4 lines of the list, then the first
#two lines again, then MAKE IT STOP
My answers and what they produce:
counter = 0
while counter <= lines_of_sanity:
for item in (lyrics):
print(item)
counter += len(lyrics)
print("MAKE IT STOP")
We tested your code with 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. We expected your code to print this:
I wanna be your endgame I wanna be your first string I wanna be your A-Team I wanna be your endgame, endgame I wanna be your endgame I wanna be your first string MAKE IT STOP
However, it printed this:
I wanna be your endgame I wanna be your first string I wanna be your A-Team I wanna be your endgame, endgame MAKE IT STOP
On this one it seems it's just printing one less line than lines_of_sanity
or my first attempt was:
counter = 0
while counter <= lines_of_sanity:
for item in (lyrics):
print(item)
counter += 1
print("MAKE IT STOP")
Which seems to print every item in the list twice (till lines_of_sanity is exceeded) which gives me 8 lines
Upvotes: 0
Views: 1553
Reputation: 1
I over thought the crap out of this at first, the answer is quite simple
count = 0
while lines_of_sanity > count:
for line in lyrics:
count += 1
if lines_of_sanity >= count:
print(line)
print("MAKE IT STOP")
you need to make a conditional that will prevent printing all 4 lines of the current lyrics. So I added an if statement before we print any line.
Upvotes: 0
Reputation: 1
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 = 2
`while lines_of_sanity > len(lyrics):
for i in lyrics:
print(i)
lines_of_sanity -= len(lyrics)
for i in range(len(lyrics)):
print(lyrics[i])
print("MAKE IT STOP")
`
Upvotes: 0
Reputation: 2737
Edit: Simplified my answer upon further reflection - this is a good problem to solve with %
, the modulo operator. What the modulo operator does is return the remainder after dividing the first operand by the second operand. While not obvious, the modulo operator is ideal for cycling an index over a list.
Example:
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
for i in range(lines_of_sanity):
print(lyrics[i % len(lyrics)])
print("MAKE IT STOP")
Output:
I wanna be your endgame
I wanna be your first string
I wanna be your A-Team
I wanna be your endgame, endgame
I wanna be your endgame
I wanna be your first string
MAKE IT STOP
Upvotes: 2
Reputation: 33
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
#divide our problem in 2 parts
#In first part printing all strings of list
#if lines_of_sanity more than size of list.
while lines_of_sanity > len(lyrics):
for i in lyrics:
print(i)
lines_of_sanity -= len(lyrics)
#In second part printing remaining strings
for i in range(lines_of_sanity):
print(lyrics[i])
print("MAKE IT STOP")
Upvotes: 0