Number Guessing Game

Random numbers, Loops, Conditionals

number = rand(1..10)
attempts = 0

puts "Guess the number (1-10):"

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

  if guess == number
puts "🎉 Correct! You guessed it in #{attempts} tries."
break
elsif guess < number
puts "Too low! Try again."
else
puts "Too high! Try again."
end end

Explanation:

  • rand(1..10) generates random number.
  • Keeps track of attempts.
  • Gives hints (too low, too high).

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *