...

/

Solution: Number Guessing Game

Solution: Number Guessing Game

Go over the implementation of the number guessing game.

Courtney's version

puts "I have a secret number (0-9) Can you guess it?" 
count = 0
input = nil # nil means no value
answer = rand(10)
until input == answer 
  input = gets.chomp.to_i 
  count += 1
  if input > answer
    puts "TOO BIG" 
  elsif input < answer
    puts "too small" 
  else
    puts "Correct!" 
  end
end
puts " The number is : #{answer}. and you guessed #{count} times!!"
Courtney's version of number guessing game

Explanation

  • Line 3: The input is initialized with a nil, which is used to represent absence of any value.

  • Line 6: The until loop stops looping when input == answer evaluates to a true condition.

  • Line 10: We can check more than one condition using the elsif block immediately after the if block. ...