# 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 ‘_________________’
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 ‘__________________’
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.

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

Apart from other suggestions, here some are some notes:

On Thu, Sep 2, 2010 at 3:28 PM, Evan R. [email protected]
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

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 ‘_________________’
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.

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. [email protected]
Date: Thu, 2 Sep 2010 08:28:42 -0500
To: ruby-talk ML [email protected]
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 ‘_________________’
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 ‘__________________’
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.

2010/9/2 Jesús Gabriel y Galán [email protected]:

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

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!. 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 ‘_________________’
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 ‘__________________’
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.

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