Python Forum
Problem with nested loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with nested loops
#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.


#Add your code here! Using the initial inputs from above, this
#should print 9 lines: all 4 lines of the list twice, followed
#by MAKE IT STOP
This is the code i have so far, its driving me insane i cant figure out what to do next:
line_count = 0
for count in range(lines_of_sanity):
    for lines in lyrics:        
      if line_count <= lines_of_sanity:
        line_count += 1
        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 MAKE IT STOP
i am one lyric shy if getting the right output and i've been stuck here for two days.
I know if i just knew what i was missing and/or could see where i messed up lol, but i cant.
Reply
#2
Please show remainder of code, for example lines_of_sanity definition, lyrics, etc.

Please Give us something that we can run
robert5502001 likes this post
Reply
#3
Hello,

Could you please condense all of the information at the top and give us a concise overview of the issue you're having? Looking at this, I am struggling to understand where the problem is.

Kind regards,
James
robert5502001 likes this post
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#4
#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).
lines_of_sanity = 6
lyrics =["I wanna be your endgame", "I wanna be your first string",
"I wanna be your A-Team", "I wanna be your endgame, endgame"]
line_count = 0
for count in range(lines_of_sanity):
    for lines in lyrics:        
      if line_count <= lines_of_sanity:
        line_count += 1
        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 MAKE IT STOP
thanks for replying :) , hope i have provided what you asked for. i do apologize for taking so long to reply.
i have just started again working with it so i havent solved the problem yet. i am still one line shy of the correct output.
Reply
#5
Is this it?
lines_of_sanity = 6
lyrics =["\nI wanna be your endgame", "I wanna be your first string",
"I wanna be your A-Team", "I wanna be your endgame, endgame"]
for count in range(lines_of_sanity):
    line_count = 0
    for lines in lyrics:        
      if line_count <= lines_of_sanity:
        line_count += 1
        print(lines)
print("\nMAKE IT STOP")
robert5502001 likes this post
Reply
#6
Quote: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)

So task at hand is how many times should be verse printed. Simple calculation will give the needed number and then we just print, no need to count lines:

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
repeats = lines_of_sanity // len(lyrics) + (0 < lines_of_sanity % len(lyrics)) 

for repeat in range(repeats):
    print(*lyrics, sep='\n')

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 MAKE IT STOP
robert5502001 likes this post
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
#7
Hi @robert5502001 ,
You ar not the first one with this assignment. Please read topic Need help on this nested loop task.
Reply
#8
(Aug-04-2021, 09:24 PM)robert5502001 Wrote: 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 dzwonki na telefon 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.


#Add your code here! Using the initial inputs from above, this
#should print 9 lines: all 4 lines of the list twice, followed
#by MAKE IT STOP
This is the code i have so far, its driving me insane i cant figure out what to do next:
line_count = 0
for count in range(lines_of_sanity):
    for lines in lyrics:        
      if line_count <= lines_of_sanity:
        line_count += 1
        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 MAKE IT STOP
i am one lyric shy if getting the right output and i've been stuck here for two days.
I know if i just knew what i was missing and/or could see where i messed up lol, but i cant.
How did you do that? It's awesome!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  computing average in nested loops cap510 5 5,186 Sep-11-2020, 12:33 PM
Last Post: deanhystad
  Capitalize first letter of names in nested loops student_of_python 9 4,750 Oct-27-2019, 07:51 AM
Last Post: Larz60+
  nested for loops to recursion ashkea26 4 3,504 Nov-02-2018, 05:00 PM
Last Post: ichabod801
  Problem with for loops erfanakbari1 4 3,300 Sep-17-2018, 02:55 PM
Last Post: gruntfutuk
  Nested loops in openCV JimmyJangle 1 4,828 Apr-17-2018, 04:10 AM
Last Post: Mekire
  Nested for loops with numbers Liquid_Ocelot 7 5,866 Aug-15-2017, 06:38 AM
Last Post: nilamo
  Nested loops, lists and if statements Liquid_Ocelot 10 8,987 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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