# Ruby guessing game

Hello! I am making a numbers guessing game that, when the user repeats a
again!”

So far I have what is below. Any help is much appreciated!

number = rand(1…10)
num_guesses = 0

puts “I’m thinking of a number from 1 to 100. Try to guess it!”

loop do
guess = gets.chomp.to_i
num_guesses += 1

``````unless guess == number
message = if guess > number
``````

else
puts “Your guess is too low.”
end

puts message
else
puts “You guessed right!”
puts “It took you #{num_guesses} tries to guess my number!”
exit
end
end

Anna Kief wrote in post #1179606:

Hello! I am making a numbers guessing game that, when the user repeats a
again!”

So far I have what is below. Any help is much appreciated!

number = rand(1…10)
num_guesses = 0

puts “I’m thinking of a number from 1 to 100. Try to guess it!”

loop do
guess = gets.chomp.to_i
num_guesses += 1

``````unless guess == number
message = if guess > number
``````

else
puts “Your guess is too low.”
end

puts message
else
puts “You guessed right!”
puts “It took you #{num_guesses} tries to guess my number!”
exit
end
end

number = rand(1…100)
puts ‘Random number between 1 and 100, try and guess what it is’
guess = gets.to_i
attempts = 1
until guess == number do
if guess < number then puts ‘Too small, try again’
elsif guess > number then puts ‘Too big, try again’
end
attempts += 1
guess = gets.to_i
end
puts “You guessed my number in #{attempts}
attempt#{‘s’ if attempts > 1}!”

though - when the user guesses the same number, it needs to return
“Already guessed. Guess again!” Do you know how I would do that?

You need to keep all the previous guesses somewhere, such as an Array.
Then you can test whether the Array already contains the value.

Okay cool, I’ll play around with this. Thanks Joel!

Anna Kief wrote in post #1179749:

Okay cool, I’ll play around with this. Thanks Joel!

Here is my attempt, but I must be doing something wrong, as it messes up
the high/low function. Any guidance on where I am going wrong?

number = rand(1…10) #defines a random number between 1 and 100
puts “I’m thinking of a random number between 1 and 100. Can you guess
what it is?”

guess = gets.to_i #converts string into integer
attempts = 1

array = []
guess = ’ ’
while guess != ’ ’
guess = gets.chomp
array.push guess

until guess == number do #until the user guesses the number
if guess = array then puts “You already guessed that. Try again!”
elsif guess < number then puts “Too low, try again!”
elsif guess > number then puts “Too high, try again!”
end
attempts += 1 #allows for other attempts
guess = gets.to_i #converts string into integer
end
end

puts “You guessed my number in #{attempts}
attempt#{‘s’ if attempts > 1}!”

This line: “if guess = array” assigns the value of array to the variable
guess.

I think you meant “if array.include?(guess)”, but because you push guess
into the array immediately, the array will always include guess.

What you need to do is test the array first, then push guess into the
array if it isn’t already included.

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