FAQ: Loops & Iterators - Loop the Loop with Loop

This community-built FAQ covers the “Loop the Loop with Loop” exercise from the lesson “Loops & Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Loop the Loop with Loop

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

can someone break this code down for me and explain. I took hours upon hours and I could not figure it out so I requested for the solution and now that I have it, I can see why I wouldn’t of never understood it.

i = 0
loop do
print “Ruby!”
i += 1
break if i == 30
end

I don’t understand how it loops “Ruby!” and then it is says that 0 (i) is added 1 and assigned back to 0 and then to break if it equals to 30. How do these numbers tell the loop to keep “looping” Ruby! 30 times?

9 Likes

Hello!

From what I understand,
print “Ruby!”
will happen once every time the loop happens.
*check if looping condition is true (keeps happening until i == 30, starting with i being 0)
*if true, print “Ruby!”
*increase i by 1 (at this point, i is 1)
*this is the end of loop.
*now, back from step 1! check if looping condition is true…

  • if true, print “Ruby!”
    *increase i by 1 (at this point, i is 2)

…and so forth, until at the point that i is 30, by which we would have already printed “Ruby!” 30 times,
the loop will BREAK and stop executing.
print “Ruby!” is inside the loop so once the loop breaks it will no longer print “Ruby!”

Here is a different approach to the question that will give you the same results
(I think you were trying to understand the question like this, which is where you might have been thrown off by the solution but at the end of the day, the result is the same x30 “Ruby!”):

m=0
loop do
m+=1
break if m==30
end

m.times{print “Ruby!”}

Here, I made a loop to get a number m to become 30
and used the .times method to print “Ruby!” instead of printing “Ruby!” within a loop.

5 Likes

Thank you! This was very helpful :pray:t2:

Hy, I made using this formula

m = 0
loop do
m +=1
print “Ruby!”
break if m == 30
end

I was wondering how i and “Ruby!” are linked. How does it know to print ruby instead of just numbers when I assigned i but said print something else. My first code was like this:

m = “Ruby!”

loop do
m += 1
print m
break if m == 30
end

1 Like

def max_num(nums):
maximum = nums[0]
for number in nums:
if number > maximum:
maximum = number
return number

#Uncomment the line below when your function is done
print(max_num([50, -10, 0, 75, 20]))

I get 50 as an answer and the answer should be 75 and I don’t understand why.

I am stuck here and want to break this laptop. It’s so frustrating for me. I am a former school teacher so this is a whole new language I am trying to understand and it has been difficult at times.

1 Like

Hi!

Does anyone know why it won’t give me a green tick and allow me to continue? The code seems to be correct and it prints “Ruby” 30 times.

Thanks.

The instructions mention “Make sure to type the string exactly as shown!”

// Your code:
print "Ruby"

// It should be:
print "Ruby!"

1 Like

Thanks you so much! I can’t believe I’ve been stuck on that all morning :rofl: