Last active
August 19, 2017 16:09
-
-
Save jonathans199/fbf4f9107c39896ac2ec952167233759 to your computer and use it in GitHub Desktop.
Guess a Number between 1-100 game
This file contains 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
# clears the screen | |
system "clear" | |
#welcomes user to game | |
puts "Welcome to my game" | |
puts "what is your name?" | |
#getting uses name | |
name = gets.chomp | |
# asking users name | |
puts "Hi #{name}\nCan you guess a number from 1-100" | |
#generate a random number | |
target_num = rand(100)+1 | |
# track how many guesses the player has made | |
num_of_guesses = 0 | |
# track if user has guess it correctly, and seto false by default | |
guessed_it = false | |
# start loop 1 = until num of guesses == 10 or guessed it | |
until num_of_guesses == 10 || guessed_it | |
# puts how many tries uses has left | |
puts "you have #{10 - num_of_guesses} left" | |
# get users number and convert to integer | |
user_num = gets.to_i | |
#add 1 in the num_of_guesses variable to | |
num_of_guesses += 1 | |
#2nd loop checking the conditions | |
if user_num < target_num | |
puts "#{name} Your choice was too low" | |
elsif user_num > target_num | |
puts "#{name} Your choice was too high" | |
else user_num == target_num | |
puts "#{name} Wow you have guess it " | |
#sets the guessed_it variable to true | |
guessed_it = true | |
end | |
end | |
#last loop unless user guessed the number then print this | |
unless guessed_it | |
puts "Sorry #{name} you ran out of tries the number was #{target_num}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment