This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Loops & Iterators Forum

View Course

789 points
527f37ff548c35ca4d00663b_896964917
Submitted by
Riser
almost 4 years ago

Print "Ruby!" 30 times.

counter = 0
loop do
    counter += 1 
    print "Ruby! "
    break if counter >= 30
end

That's my code, why isn't it doing it for me? It always adds nil at the after it prints ruby 30 times. Is there some silly thing I'm missing?

Alex J edited this post to fix code formatting


6 votes

permalink

Yeah, there’s a silly thing you missed: the famous Eric Weinstein wants you to

print "Ruby!"

thirty times. That’s "Ruby!". Without a trailing space.

3117 points
Picture
Submitted by
Alex J
almost 4 years ago


4 votes

permalink

string = 0
loop do
    string += 1
    print "Ruby!"
    break if string > 30
end

This is my code. In the console window, I can see "Ruby!" 30 times. But it is asking me to do print "Ruby!" 30 times & I cannot to the next exercise.

505 points
51fdae2f80ff339f660005b3_359012627
Submitted by
Shiraz
almost 4 years ago

3 Comments

51fdae2f80ff339f660005b3_359012627 Shiraz almost 4 years ago

got it :)
it should be 1 instead of 0

532640da52f8636dac0092d4_541689159 Joseph Sawyer over 3 years ago

Thank you for this.

53073a30631fe9126b0030f5_405398419 Matthew C over 3 years ago

string = 1 //or// break if string >= 30 //or// break if string == 30


2 votes

permalink

Hey guys I had the same problem here is my code that works. I have noticed that it prints an extra line at the end of nearly everything I code, so using the 29 gets me to the 30 they ask for:'

counter = 0
loop do
counter += 1
print "Ruby!"
break if counter > 29
end

74 points
4813ea6d138e8d3e94496a01f1fadd21?s=140&d=retro
Submitted by
amu300gmail.com
over 3 years ago

2 Comments

25db4313f84d78daf93d73ebfd1b6098?s=140&d=retro Nat Veiga over 3 years ago

the same happened to me. thanks!

53073a30631fe9126b0030f5_405398419 Matthew C over 3 years ago

it's not an extra line, remember your counter is starting at 0, not 1. second last line should probably be break if counter >= 30


1 vote

permalink

Should be

string = 0
loop do
string += 1
print "Ruby!"
break if string >= 30
end

372 points
Picture
Submitted by
Blake Craw
over 3 years ago

6 Comments

5483382fd3292fac3e000b65_66838181 Maddy Andrade over 3 years ago

Do you know what the difference is between writing string vs counter in this exercise? I wrote the same thing you did but just had counter instead. What you wrote worked perfectly but I just want to clarify the difference. I Thanks!

Picture Blake Craw over 3 years ago

counter would be like an integer, where string would be a word (i.e. Ruby).
Hope this helped

Picture Andrew Kocurek over 3 years ago

Not following you on this one, you say string is a word but you clearly have an integer there.

52935d54abf821949d0027c8_506117216 Fridah Nyagu over 3 years ago

I dont think counter or string has any effect,It can be any letter

Da8fc2230018d9d037680c1df3cac0fa?s=140&d=retro ADS KUL about 3 years ago

string = 0
loop do
string += 1
print "Ruby!"
break if string >= 30
end

Da8fc2230018d9d037680c1df3cac0fa?s=140&d=retro ADS KUL about 3 years ago

worked for me


0 votes

permalink

counter = 0
loop do
counter += 1
print "Ruby!"
break if counter >= 30
end

946 points
52935d54abf821949d0027c8_506117216
Submitted by
Fridah Nyagu
over 3 years ago


0 votes

permalink

x = 1
loop do
x +=1
print "Ruby!"
break if x >= 31
end

This worked for me

313 points
748d7269ee9ba9922233d8e55e72c483?s=140&d=retro
Submitted by
KHALID_ABDELAZIZ2001YAHOO.COM
over 2 years ago