Created
November 27, 2011 15:06
-
-
Save ngm/1397669 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks - Ruby - Day 1 - Guess the number
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 "\nHello. What's your name?" | |
print "[Your name]: " | |
name = gets.chomp | |
puts | |
puts "Hello #{name}." | |
theMagicNumber = rand(100)+1 | |
puts "\nListen. I've got a number between 1 and 100." | |
puts "\nGuess the number, #{name}." | |
number_has_been_guessed = false | |
until number_has_been_guessed | |
print "\n[Your guess]:" | |
guess = gets.to_i | |
puts | |
if guess > theMagicNumber | |
puts "Sorry #{name}, #{guess} is too high." | |
print "\nGuess again." | |
elsif guess < theMagicNumber | |
puts "Sorry #{name}, #{guess} is too low." | |
print "\nGuess again." | |
else | |
puts "You got it! The magic number is #{theMagicNumber}." | |
number_has_been_guessed = true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment