Skip to content

Instantly share code, notes, and snippets.

@ashleytbasinger
Created August 20, 2013 01:53
Show Gist options
  • Save ashleytbasinger/9ff5ee0948c44d722282 to your computer and use it in GitHub Desktop.
Save ashleytbasinger/9ff5ee0948c44d722282 to your computer and use it in GitHub Desktop.
guess the number game for ruby
number = rand(0...100)
puts "Let's play a game!"
print "Guess a number between 0 and 100:"
loop do
guess = gets.chomp
if !/^\d+$/.match(guess)
puts 'Invalid guess, try again...'
print "guess again: "
elsif guess.to_i > number
puts "too high"
print "guess again: "
elsif guess.to_i < number
puts "too low"
print "guess again: "
else guess.to_i == number
puts "correct"
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment