Advertisement
iAnonGuy

~ Hello, Ruby. ~

Jan 17th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.94 KB | None | 0 0
  1. # "Hello, Ruby!" ((=
  2.  
  3. # Getting user's name & greeting them
  4. puts "Welcome to 'GuessTehNum'"
  5. print "What's your name? ~ "
  6. userName = gets.chomp
  7. puts "Welcome, #{userName}!"
  8.  
  9. # Generating a random number for the player to guess
  10. puts "\nI've got a random number from 1-100, Can you guess it? :D"
  11. rand = rand(100) + 1
  12.  
  13. # Counting user's guesses
  14. score = 0
  15. won = false
  16.  
  17.     while won == false && score < 10
  18. puts "Let's begin, You have #{10 - score} tries left!"
  19. print "Make a guess ~ "
  20. guess = gets.to_i
  21. score += 1
  22.  
  23. # Comparing user's guess with teh randnum and printing appropriate message
  24. if guess < rand
  25.     puts "Oops, Too Low! :P"
  26. elsif guess > rand
  27.     puts "Oops, Too High! :P"
  28. elsif guess == rand
  29.     puts "Good Job, #{userName}!\nYay, You guessed my number in #{score} guesses!"
  30.     won = true
  31. end
  32. end
  33. # Revealing teh number in case user ran out of tries
  34. unless won
  35.     puts "Sorry, You lost! :D\nPS: My number was #{rand}!"
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement