Skip to content

Instantly share code, notes, and snippets.

@faizaanshamsi
Last active December 28, 2015 08:49
Show Gist options
  • Save faizaanshamsi/7474351 to your computer and use it in GitHub Desktop.
Save faizaanshamsi/7474351 to your computer and use it in GitHub Desktop.
Random Number Guessing Game
MAX = 30
random_number = rand(MAX+1) #This makes the range of possible number 0 to MAX, inclusive.
user_correct = false
while user_correct == false do
print "Guess number between 0 and #{MAX}: "
user_guess = gets.chomp
if ((0..MAX).include?(user_guess) == false) || user_guess[/\D/] == true
puts "Invalid input, must enter a number between 0 and #{MAX}."
end
if user_guess == random_number
user_correct = true
puts "Congratulations, you guessed the number!"
elsif user_guess < random_number
puts "Too low, try again!"
else
puts "Too high, try again!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment