Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Last active December 28, 2015 08:59
Show Gist options
  • Select an option

  • Save jmoon90/7475602 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/7475602 to your computer and use it in GitHub Desktop.
Guessing game between the range(1..1000)
actual_number = rand(0..1000)
puts "Can you guess the number I am thinking?"
print "It is between 0 and 1000: "
guess = 0
def invalid?(user_input)
!!user_input.to_s.match(/\A\d+\z/)
end
while actual_number != (guess)
guess = gets.chomp
if invalid?(guess) == true
if guess.to_i == actual_number
puts "Congratulations, you\'ve guessed the number"
break
elsif guess.to_i > actual_number
puts "Too high, try again..."
else
puts "Too low, try again..."
end
else
puts "Number only please"
end
print "Try again =>"
end
@jmoon90
Copy link
Copy Markdown
Author

jmoon90 commented Nov 14, 2013

If you try to enter anything besides number it will tell you to "number only." You must only put numbers in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment