Python Forum
Need help on this nested loop task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help on this nested loop task
#1
Bug 
Here is the task:
"
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"

Here is my code:


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
lines = 0
for lyric in lyrics:
    while lines < lines_of_sanity:
        lines += 1
    print(lyric)
print("MAKE IT STOP")
The challenge I am facing:
I am trying to stop the loop when the lines of the lyric reach "6". However, I have no clue how to get there.

Thank you so much for taking your time to help me!
Thanks in advance! Big Grin
buran write Apr-07-2021, 05:21 AM:
Thread moved to Homework
Reply
#2
(Apr-07-2021, 02:03 AM)PP9044 Wrote: for lyric in lyrics:
... is correct. But then you start a while loop that makes no sense. You must just print the "lyric" line. And then you must increment the "lines" counter. After that you must test if "lines" equals "lines_of_sanity". If that is the case you have to print: "MAKE IT STOP" and after that you add a "break" statement to exit the "for" loop.
PP9044 likes this post
Reply
#3
Lets start from the beginning.

You have given two things.

1. Array with four items in it.

2. A integer variables.

The homework problem said print every element in array x 6 times. Then print something else.

First build a for else loop that refer to line of sanity variable.
#For loop with else while

Then build a separate loop that prints all items in array.

If you can build those loops, then try to nest one for loop with in another.

When I saw this problem, I didn’t know this. But I know python have some loop functions. So visited here to look up.
https://www.w3schools.com/python/python_for_loops.asp

Learn to break down one big problem to smaller chucks and then solve the parts.

The range() Function
This is how to use integer variable in for loop.

Else in For Loop
This is how you break out of loop.

Nested Loops
This is how you nest one for loop with another.

Build it in order separately. After that combine.
Reply
#4
If you want to stick with your code,
Nest for loop with in while loop.

Instead of inserting while loop with in for loop.

I challenge you to build it only using nested for loop!
Reply
#5
It is always good idea to start with clarity in mind. What is required:

Quote: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).

Using the initial inputs from above, this
should print 9 lines: all 4 lines of the list twice, followed
by MAKE IT STOP"

So task is actually: print all items in list x times where x is rounded up division of lines_of_sanity and number of lines in verse (i.e. length of list).

As this is homework just pieces and bits:

>>> list_ = ['bacon', 'spam']
>>> print(*list_, sep='\n')
bacon
spam
>>> for _ in list_:
...     print(*list_, sep='\n')
...
bacon
spam
bacon
spam
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested if stmts in for loop johneven 2 5,838 Oct-19-2019, 04:05 AM
Last Post: xeedon
  Nested for loop issue always using index 0 searching1 2 2,552 Dec-30-2018, 09:17 AM
Last Post: searching1
  nested while loop flow help Ponamis 4 2,921 Nov-02-2018, 11:22 PM
Last Post: Ponamis
  Nested loop Tryhard20 3 5,660 Sep-05-2018, 04:57 AM
Last Post: volcano63
  Nested Loop multiplication table SushiRolz 3 10,154 Feb-28-2018, 04:34 AM
Last Post: Larz60+
  Nested Loop to Generate Triangle Babbare 12 11,606 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