Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Loops Ruby Loops The While Loop

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Guys what did i do differently thats making the loop not work properly.

def print_hello(number_times)
   i = 0
     while number_times < 0
   puts "hello"
 i += 1
    end
 end


 answer = 0

 while answer < 5
 print "How many times do you want to print 'hello'? Enter a number greater than 5 to exit) "
     answer = gets.chomp.to_i
 print_hello(answer)
 end

its not printing "Hello" the amount of times.

3 Answers

You're incrementing i but testing number_times for being less than 0.

Do you want to test

i < number_times

then stop the loop?

My guess, and I don't have a workspace open, is something like this:

def print_hello(number_times)
   i = 0
   while i < number_times
     puts "hello"
     i += 1
  end
end

answer = 0

while answer < 5 
  print "How many times do you want to print 'hello'? Enter a number greater than 5 to exit) "
  answer = gets.chomp.to_i
  print_hello(answer)
end

Let me know how you get on.

Steve.

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Hey man thanks for responding, your code worked tho im not gonna lie im still at a loss. Like the while loop with gets.chomp i get however the while loop withing the print_hello method i dont get. I am havibg trouble seeing how both while loops are synergizing with eachother. Like couldnt i have just put print "hello" in the define method an worked with it through the while loop on he bottom?

Hi Alphonse,

Apologies for the delay in responding - I was busy last night on other things.

OK, so there's two whle loops. One controls user input; as soon as the user enters something greater than 5, the program terminates. That's a bit of a weird way of doing it, but hey, that's what we've got.

The other while loop is inside the print_hello method. That method takes one parameter which is the user input (as long as it wasn't more than 5!); so it is a number from 1 to 4, we hope. That is called number_times inside this method.

Inside the method, we create an index or counter, called i and set it to zero. We then enter a while loop and increment i each time the loop is repeated. That's the i += 1 bit. The condition that maintains the loop is i being less than number_times. As soon a i becomes equal to number_times, the loop terminates and we return back to the place in code where the method was called from, i.e. right back in the first loop!!

Let's walk this through. The program starts and the user is prompted for a number. He enters "3". That string has its whitespace removed (chomp) and is converted into an integer(to_i). We then call the print_hello method passing in the user's answer (3) as a parameter. Execution jumps into the method where number_times is assigned the value 3 which was passed across. We set i to zero and enter the while loop if i is less than number_times. At the moment, i is zero and number_times is 3 - the loop excutes. Inside the loop, we print "hello" to the screen, add one to i and repeat. Now, i is equal to 1 which is still less than three so we print "hello" to the screen, add one to i and repeat. Now, i is 2; we do that all again. Now, i is 3 which is NOT less than number_times - the loop stops, we exit the print_hello method and fall back to the first while loop which goes round again and prompts the user for a number.

There are much nicer ways of doing this, in reality, but that wouldn't teach you about while loops. This is designed to teach you that, not to write pretty code, just yet. For example, if the user entered 7 at the first prompt, the print_hello method would be called, "hello" would be printed 7 times, then the loop would exit so this really isn't working nicely at all but it demonstrates while loops well as a concept, not as an implementation.

I hope that helps!

Steve.

Hi Alphonse,

Here is the code it appears you are using:

def print_hello(number_times)
  i = 0
  while number_times < 0
    puts "hello"
    i += 1
  end
end

answer = 0

while answer
  print "How many times do you want to print 'hello'? Enter a number greater than 5 to exit) "
  answer = gets.chomp.to_i
  print_hello(answer)
end

If I have that correctly, then it appears your issue lies within the print_hello method. There you perform a comparison: number_times less-than 0. You can expect the user to always input a number larger than 0 when prompted to do so, therefore the answer variable will usually be a value greater-than 0. The answer variable is then passed to the print_hello method, which checks if number_times less-than 0, which it never will be, therefore the while loop is skipped and puts "hello" is never evaluated.

Try changing the code inside the print_hello method, and that should solve your issue.

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Whats the point in making two while loops here if you dont mind me asking?

You'd have to ask Jason exactly why he decided to use 2 while loops for this lesson, but I believe he was simply using repetition as a learning tool. The same end result can be achieved using different code, and without the need for 2 while loops. But, using it twice helps you understand how they work.

There's another nice lesson in there about one of Ruby's core philosophies: There's more than one way to do the same thing. Feel free to produce the same results with totally different code.