Last active
December 28, 2015 08:59
-
-
Save jmoon90/7475602 to your computer and use it in GitHub Desktop.
Guessing game between the range(1..1000)
This file contains hidden or 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
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you try to enter anything besides number it will tell you to "number only." You must only put numbers in.