Skip to content

Instantly share code, notes, and snippets.

@ngm
Created November 27, 2011 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngm/1397669 to your computer and use it in GitHub Desktop.
Save ngm/1397669 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks - Ruby - Day 1 - Guess the number
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