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

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

My counter wont increase any ideas on why?

print "Enter a number greater then ten." counter = 1 loop do number = gets.chomp.to_i if number.to_i < 10 puts " #{counter}.sorry that's not higher then ten." elsif number.to_i > 10 puts" #{counter}.thank you " break counter = 1+1 end end print"That is a sufficient number"

2 Answers

Marc Schultz
Marc Schultz
23,356 Points

If I reformated your code correctly then please have a look at the inline comments:

print "Enter a number greater then ten."
counter = 1
loop do
    number = gets.chomp.to_i
    if number.to_i #1
        puts " #{counter}.sorry that's not higher then ten."
    elsif number.to_i > 10 #2
        puts " #{counter}.thank you "
        break #3
        counter = 1+1 #4
    end
    #5
end
print"That is a sufficient number"
  1. if number.to_i is not correct because it just tests if there is a value. And because you type in a value right before, this statement is always true. So you'll never reach the elsif block.
  2. You don't need to cast via .to_i. number is already of type integer because of your previous cast gets.chomp.to_i.
  3. Everything after a break cannot be reached. So your counter incrementation will never be executed.
  4. counter = 1+1 cannot work. 1+1 is always 2, not matter how often your loop iterates. Instead you have to use the old counter value, increment it by 1 and reset it to the same counter variable. Then it looks like this: counter = counter + 1 The short form is counter += 1.
  5. Your counter variable can only be incremented in the case the elsif block is reached. As I described in point 1, you cannot reach the elsif block. So your counter variable will never get incremented. Because you use #{counter} in both if blocks, I assume you want to show the incrementation of the counter variable no matter if you put in a number below 10 or higher. Then you have to move your incrementation out of the if blocks and put it at the end of the loop block.

One possible solution could be:

print "Enter a number greater then ten: "
counter = 1
loop do
    number = gets.chomp.to_i
    if number > 10
        puts " #{counter}. Thank you!"
        break
    else
        print " #{counter}. Sorry that's not higher then 10! Try again: "
    end
    counter += 1
end
puts "That is a sufficient number"

Output:

Enter a number greater then ten: 1
 1. Sorry that's not higher then 10! Try again: 5
 2. Sorry that's not higher then 10! Try again: 3
 3. Sorry that's not higher then 10! Try again: 11
 4. Thank you!
That is a sufficient number
Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Hey man i tied it an it appears to still not show the number increasing

Tim Eckert
Tim Eckert
7,011 Points

Try counter = counter + 1

Marc Schultz
Marc Schultz
23,356 Points

I reorganized my answer and put more explanations underneath your code. The numbers as inline comments show you where I'm pointing at.

Andres Osorio
Andres Osorio
3,830 Points

I don't quite understand your code. So, let me see if I get it:

You want the user to enter a number greater than 10. If the user inputs a number lower than 10, you'll simply ask again for a correct input. If the number is greater than 10, then the program finishes. I guess the counter variable will count the number of attempts the user inserts a number until it's greater than 10.

is that right?

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Yes correct. And your syntax worked by the way. I just typed it wrong mind i ask why the counter +=1 goes after i end the statement then you added a end after counter +=1. That part greatly loses me. Appreciate the help.

Marc Schultz
Marc Schultz
23,356 Points

He is not the guy with the counter += 1.