Created
October 28, 2011 12:47
-
-
Save jomontanari/1322183 to your computer and use it in GitHub Desktop.
Ruby - Day 1 - bonus problem (7l7w)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "What's the maximum number?" | |
max_num = gets().to_i | |
number_to_guess = rand(max_num + 1) | |
puts "Thinking of a number between 0 and #{max_num} ... OK." | |
guess = nil | |
while guess != number_to_guess do | |
puts "Please make a guess ..." | |
guess = gets().to_i | |
puts "Too high!" if guess > number_to_guess | |
puts "Too low!" if guess < number_to_guess | |
end | |
puts "Well done! You got it!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same comment as on Mary-annes code, you can eliminate the guess assignment by writing
while (guess ||= nil) != number_to_guess do
The trailing 'if' clause feels more rubyish than an
if...elsif
statement.