From: "David A. Black" Date: 2009-10-11T19:24:53+09:00 Subject: Re: Numbers game - multiple guesses? Hi -- On Sat, 10 Oct 2009, Miguel Regedor wrote: > 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}." 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)