Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested loop
#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 = 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



Tried many times but still not able to get the correct output
Reply
#2
Can you show us your last code attempt (in Python code tags), the expected result and actual result (both in output tags)?
Reply
#3
for i in range(lines_of_sanity):
	for lines in lyrics:
		print(lines)
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 I wanna be your A-Team I wanna be your endgame, endgame 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 I wanna be your A-Team I wanna be your endgame, endgame 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 I wanna be your A-Team I wanna be your endgame, endgame MAKE IT STOP
Reply
#4
You have to repeat inner cycle till you print lines_of_sanity lines - not run it lines_of_sanity times.

You will still need two nested loops - the inner is nearly OK.
Hints
  • You will have to count number of lines your print.
  • You will have to break out of both loops when you print required number of lines
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Need help on this nested loop task PP9044 4 4,637 Apr-16-2021, 01:31 PM
Last Post: perfringo
  Nested if stmts in for loop johneven 2 5,866 Oct-19-2019, 04:05 AM
Last Post: xeedon
  Nested for loop issue always using index 0 searching1 2 2,573 Dec-30-2018, 09:17 AM
Last Post: searching1
  nested while loop flow help Ponamis 4 2,957 Nov-02-2018, 11:22 PM
Last Post: Ponamis
  Nested Loop multiplication table SushiRolz 3 10,199 Feb-28-2018, 04:34 AM
Last Post: Larz60+
  Nested Loop to Generate Triangle Babbare 12 11,684 May-29-2017, 05:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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