 # Numbers game - multiple guesses?

I’m trying to figure out how to get a user to be able to submit guesses
in a guessing game multiple times.

This is what I have so far:

print "I am thinking of a number between 1 and 10 , can you guess what
it
is? "

num = gets
num = num.chomp
num = num.to_i

if num == 4
puts “You got it!”
elsif num < 4 then
puts “Guess Again”
elsif num > 5 then
puts “Guess Again”

How do I make it so they can keep guessing until they get it right?

Thanks

you can do this by making an infinite loop and break from the loop when
the
right guess comes

print "I am thinking of a number between 1 and 10 , can you guess what
it is? "

while true
num = gets
num = num.chomp
num = num.to_i

if num == 4
puts “You got it!”
break
elsif num < 4 then
puts “Guess Again”
elsif num > 5 then
puts “Guess Again”
end
end

2009/10/10 John K. [email protected]

Use a control loop such as while(!correct) that wraps your guessing
logic.

John K. wrote:

num = num.chomp

How do I make it so they can keep guessing until they get it right?

Thanks

There seem to be a slight bug in the if clause, what happens if the
number is 5?

There are many ways to do ask for multiple guesses, here is one way to
do it
without a loop construct, with the if clause corrected.

catch :right_guess do
num = gets
num = num.chomp
num = num.to_i

``````if num == 4
puts "You got it!"
throw :right_guess
elsif num < 4 then
puts "Guess Again"
redo
elsif num > 4 then
puts "Guess Again"
redo
end
``````

end

Kind Regards,
Rajinder Y.

http://DevMentor.org
Do Good ~ Share Freely

Great! Thanks!

John K. wrote:

I’m trying to figure out how to get a user to be able to submit guesses
in a guessing game multiple times.

Hi John. There are bunches of ways you could do this, but here is what
I’d do.

#################### CODE
###############################################

puts “I’ve picked a number between 1 and 10. See if you can guess it!”

until 4 == gets.chomp.to_i
puts “WRONG! Try again.”
end

puts “You got it!”

################## END CODE
#############################################

UNTIL is like a while loop but it loops ‘until’ a condition is met. In
other words, WHILE will loop as long as a condition is TRUE, and UNTIL
will loop until a condition is FALSE.

You could also write the until line as:

``````        while 4 != gets.chomp.to_i
``````

and it would be the same thing.

I noticed that I didn’t use a single variable in that code. If you
wanted,
you could have used a variable in place of the constant fixnum 4, and
just assigned a value to it.

functional, are
not very pretty. Since all you are doing is looking for the CORRECT
answer, you could have written …

if secret_number == guessed_number
print “You guessed it!\n”
else # this covers all other cases.
print “Nope! Guess again.\n”
end

And that would be simpler. ifelse in general is an ugly construct in my
opinion and so I tend to choose CASE when I need to evaluate multiple
conditions.

Hope this helps!

I would do the guessing game, this way:

number = 5 + rand(65)
puts “Try to guess the Magic NuMbeR is between 5 and 69”

begin
user_try = gets.chomp.to_i
puts “The NuMbeR is #{user_try > number ? ‘smaller’ : ‘bigger’} than
#{user_try}! Try again:” unless user_try == number
end until user_try == number

puts “Congratulations! You found the Magic NuMbeR! The NuMbeR is #
{number}.”

Hope it helps.

Kind Regards,
Miguel R.

Hi –

On Sat, 10 Oct 2009, Miguel R. wrote:

end until user_try == number

puts “Congratulations! You found the Magic NuMbeR! The NuMbeR is #
{number}.”

Just for fun, here’s a little rewrite:

number = 5 + rand(65)

print "Try to guess the Magic NuMbeR between 5 and 69: "

diff = { false => “bigger”, true => “smaller” }

loop do
user_try = gets.to_i
break if user_try == number
puts “The NuMbeR is #{diff[user_try > number]} than #{user_try}!”
print "Try again: "
end

puts “Congratulations! You found the Magic NuMbeR!”
puts “The NuMbeR is #{number}.” David

The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

Miguel R. wrote:

I like the diff hash clever!

diff = {
-1 => proc { puts “The number is larger” },
0 => proc { throw(:done) },
1 => proc { puts “The number is smaller” },
}

catch(:done) { loop { diff[gets.to_i <=> number][] } }

I like the diff hash clever!

cheers

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.