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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Hi, I was just exploring the loop function with gets.chomp syntax, but it seems like the gets.chomp input doesn’t work in the loop function. Is there a reason for this?
This is how my code looks like:
print "Tell me your age."
age = Integer(gets.chomp)
loop do
age += 1
print "#{age}"
break if age <= 30
end
Instead of looping up to 30, it only looped up to 26. Why is it so? Is there another way to make this work?
I think that the problem is caused by this condition.
Let’s say that I am 26 years old, age = 26. Now we get into the loop block, age += 1 results in age = 26 + 1 = 27. In the next line of the code 27 is printed out to the console. And now if 26 <= 30 (which is true) we break and the loop stops.
And if my input is 55 then we have an infinite loop.
The condition should be probably changed to age >= 30.
SOLVED
When completing the loop method, my “loop” and “break” is entering orange like a variable:
i = 20
loop do
i -= 1
print “#{i}”
break if i <= 0
end
and the console is printing:
191817161514131211109876543210
which i would have expected to print vertically.
why is this happening, the task is registering as complete and correct, it just doesn’t correlate with the rest of the lessons.
I wouldn’t pay much attention to the colors you find on Codecademy. These were picked by the developers, but outside of this environment, colors could be completely different.
I understand that it can be puzzling, sometimes. Especially when the lesson’s example gives you loop do in blue, whilst showing up in orange in the editor… Just remember the above comment.
print "#{i}"
wouldn’t print the result vertically. For it to appear that way, you’ll want to use a new line "\n".
So, you could do this
print "#{i}\n"
or, you could simply use puts, instead of print.
The primary difference between puts and print is that puts adds a newline after executing, and print does not.