Solution: Number Guessing Game
Go over the implementation of the number guessing game.
We'll cover the following...
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 anil
, which is used to represent absence of any value.Line 6: The
until
loop stops looping wheninput == answer
evaluates to atrue
condition.Line 10: We can check more than one condition using the
elsif
block immediately after theif
block. ...