# New to ruby - Need help with simple guessing program

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333>\
**Category:** Ruby\
**Created:** [September 2, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333 "2010-09-02T15:28:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 2, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/1 "2010-09-02T15:28:39Z")

</div>

Ok,so as a little practice I’ve decided to come up with a Guessing Game  
where the user is able to guess a number, and if it the same as the  
randomly generated number by the computer, he gets a message that says a  
little hooray message. That part is done, and completed, but now I need  
help  
with tracking the amount of entries he makes in gets

Here is my current code:

random\_number = rand(10)

puts ‘Can you guess the number?’  
puts ‘Select a number from 1-10 please’

players\_number = gets.chomp

while players\_number.to\_i != random\_number.to\_i

while players\_number.to\_i \< random\_number.to\_i  
if players\_number.to\_i \< random\_number.to\_i  
puts ‘Sorry your to low’  
puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
random\_number = rand(10)

```
puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

```

end  
end

while players\_number.to\_i \> random\_number.to\_i  
if players\_number.to\_i \> random\_number.to\_i  
puts ‘Sorry your to high’  
puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
random\_number = rand(10)

```
puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

```

end  
end

while players\_number.to\_i == random\_number.to\_i  
if players\_number.to\_i == random\_number.to\_i  
puts ‘Congratluations! You Win!’  
puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
random\_number = rand(10)

```
puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

```

else

end  
end  
end

Can anyone help me with tracking the amount of entries he makes? Thank  
you.

---

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 2, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/2 "2010-09-02T15:57:03Z")

</div>

Evan R. wrote:

> Can anyone help me with tracking the amount of entries he makes? Thank  
> you.

Put these lines in the appropriate places:

tries = 0

…

tries = tries + 1

…

puts “You took #{tries} attempt(s)”

BTW your code could do with some trimming, but if it works that’s the  
first thing 🙂 Some suggestions:

- no need for inner while loops, the outer while loop handles it for  
you.

if condition1  
…  
elsif condition2  
…  
else  
…  
end

- Move the bits which are common (prompting for the next attempt) to  
outside of this section, since they’re the same in all cases

---

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 2, 2010, 4:19pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/3 "2010-09-02T16:19:11Z")

</div>

Apart from other suggestions, here some are some notes:

On Thu, Sep 2, 2010 at 3:28 PM, Evan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Here is my current code:
> 
> random\_number = rand(10)
> 
> puts ‘Can you guess the number?’  
> puts ‘Select a number from 1-10 please’
> 
> players\_number = gets.chomp

As you are always using players\_number as an integer to, I suggest  
doing the to\_i after the gets, so you don’t have to repeat it over and  
over:

players\_number = gets.chomp.to\_i

> while players\_number.to\_i != random\_number.to\_i

random\_number is already an integer.

> while players\_number.to\_i \< random\_number.to\_i

As has been said, this loop is not needed, just the if

> if players\_number.to\_i \< random\_number.to\_i  
> puts ‘Sorry your to low’  
> puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
> random\_number = rand(10)
> 
> puts ‘Can you guess the number?’  
> puts ‘Select a number from 1-10 please’

And this should be moved to a common place once.

> puts ‘Can you guess the number?’  
> random\_number = rand(10)
> 
> Can anyone help me with tracking the amount of entries he makes? Thank  
> you.

Regards,

Jesus.

---

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 2, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/4 "2010-09-02T17:19:31Z")

</div>

Evan,

As you have players\_numbers you need to setup players\_guesses

Increasing the players\_guesses every time a player enters a number.

players\_guesses += 1 // this increments players\_guess by 1

Once you have this try creating a class and building out methods… Try  
to DRY this example up. 😉

* * *

From: Evan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Reply-To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Thu, 2 Sep 2010 08:28:42 -0500  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: New to ruby - Need help with simple guessing program

Ok,so as a little practice I’ve decided to come up with a Guessing Game  
where the user is able to guess a number, and if it the same as the  
randomly generated number by the computer, he gets a message that says a  
little hooray message. That part is done, and completed, but now I need  
help  
with tracking the amount of entries he makes in gets

Here is my current code:

random\_number = rand(10)

puts ‘Can you guess the number?’  
puts ‘Select a number from 1-10 please’

players\_number = gets.chomp

while players\_number.to\_i != random\_number.to\_i

while players\_number.to\_i \< random\_number.to\_i  
if players\_number.to\_i \< random\_number.to\_i  
puts ‘Sorry your to low’  
puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
random\_number = rand(10)

```
puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

```

end  
end

while players\_number.to\_i \> random\_number.to\_i  
if players\_number.to\_i \> random\_number.to\_i  
puts ‘Sorry your to high’  
puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
random\_number = rand(10)

```
puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

```

end  
end

while players\_number.to\_i == random\_number.to\_i  
if players\_number.to\_i == random\_number.to\_i  
puts ‘Congratluations! You Win!’  
puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
random\_number = rand(10)

```
puts 'Can you guess the number?'
puts 'Select a number from 1-10 please'

players_number = gets.chomp

```

else

end  
end  
end

Can anyone help me with tracking the amount of entries he makes? Thank  
you.

---

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 2, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/5 "2010-09-02T17:25:57Z")

</div>

2010/9/2 Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> As you are always using players\_number as an integer to, I suggest  
> doing the to\_i after the gets, so you don’t have to repeat it over and  
> over:
> 
> players\_number = gets.chomp.to\_i

Or, simply: players\_number = gets.to\_i

---

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 2, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/6 "2010-09-02T19:05:56Z")

</div>

Hi Evan,

As others have said, you could have a single control loop, and run  
everything in that. Something like

won=false  
while !won  
  
if   
won=true  
end  
end

The random number in your code will change every time rand(10) is  
called, so for the user they won’t know if their next guess should be  
higher or lower. Also having a single place for input will make it  
easier to do other things for that input. (e.g. count=0 outside the loop  
and count+=1 inside, rather than multiple count+=1 for every line  
requiring user input.)

For comparison, you can use the \<=\> operator which will return -1 if  
lower, 1 if higher and 0 if they are the same. Using this combination of  
things, it is possible to code the whole game in about 20 clear lines of  
ruby and then to add 3 lines to make it keep a count and display that  
count at the end. (16 was what my code example was, others might come up  
with even fewer.)

Since this is practice for you, I’ve held off on putting any real code  
here, but as a hint, take a look at using case…when…else if you try  
with \<=\>. One good place to go to for practice code is  
[Learn How to Blog and Build Websites for Profit!](http://rubylearning.com/blog/). They set a coding challenge every month,  
aimed at new ruby developers.

Good luck and enjoy,  
Paul

Evan R. wrote:

> Ok,so as a little practice I’ve decided to come up with a Guessing Game  
> where the user is able to guess a number, and if it the same as the  
> randomly generated number by the computer, he gets a message that says a  
> little hooray message. That part is done, and completed, but now I need  
> help  
> with tracking the amount of entries he makes in gets
> 
> Here is my current code:
> 
> random\_number = rand(10)
> 
> puts ‘Can you guess the number?’  
> puts ‘Select a number from 1-10 please’
> 
> players\_number = gets.chomp
> 
> while players\_number.to\_i != random\_number.to\_i
> 
> while players\_number.to\_i \< random\_number.to\_i  
> if players\_number.to\_i \< random\_number.to\_i  
> puts ‘Sorry your to low’  
> puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
> random\_number = rand(10)
> 
> ```
> puts 'Can you guess the number?'
> puts 'Select a number from 1-10 please'
> 
> players_number = gets.chomp
> 
> ```
> 
> end  
> end
> 
> while players\_number.to\_i \> random\_number.to\_i  
> if players\_number.to\_i \> random\_number.to\_i  
> puts ‘Sorry your to high’  
> puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
> random\_number = rand(10)
> 
> ```
> puts 'Can you guess the number?'
> puts 'Select a number from 1-10 please'
> 
> players_number = gets.chomp
> 
> ```
> 
> end  
> end
> 
> while players\_number.to\_i == random\_number.to\_i  
> if players\_number.to\_i == random\_number.to\_i  
> puts ‘Congratluations! You Win!’  
> puts ‘\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_’  
> random\_number = rand(10)
> 
> ```
> puts 'Can you guess the number?'
> puts 'Select a number from 1-10 please'
> 
> players_number = gets.chomp
> 
> ```
> 
> else
> 
> end  
> end  
> end
> 
> Can anyone help me with tracking the amount of entries he makes? Thank  
> you.

---

<div class="post-metadata">

**Author:** ![chichu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chichu](https://www.ruby-forum.com/u/chichu)\
**Post date:** [September 7, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/new-to-ruby-need-help-with-simple-guessing-program/194333/7 "2010-09-07T15:19:33Z")

</div>

Thank you all for the tips, I’m loving ruby more every minute.
