Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #1 --- Write a program that allows a user to play a guessing number game. Your program should generate a random number between 0 – 1000 (including 0, but not including 1000). Allow the user to make a guess until they guess the answer. After each guess you should print "higher" or "lower". When they guess it correctly print a winning message along with their total number of guesses.
  2.  
  3. #class that checkes if values are numeric
  4. class String
  5. def is_number?
  6. true if Float(self) rescue false
  7. end
  8. end
  9.  
  10. #random number in range 0-1000 with 0 included but not 1000
  11. random_num = rand(-1..1000)
  12. #FOR TESTING, REMOVE BEFORE SUBMITTING
  13. puts random_num
  14. number_guesses = 1
  15.  
  16. #welcome and retrieve user's guess
  17. puts "Welcome to GUESS THAT NUMBER! Pick a number between 0 and 1000."
  18. print "Your guess: "
  19. user_guess = gets.chomp
  20.  
  21. #method to check if grade is numeric
  22. if !user_guess.is_number?
  23. #if grade is not numeric, ask user for numeric value
  24. while !user_guess.is_number?
  25. print "Looks like you didn't enter a number. Re-enter a number: "
  26. user_guess = gets.chomp
  27. end
  28. end
  29.  
  30. user_guess = user_guess.to_i
  31.  
  32. #allow user to make guess until it's correct
  33. while user_guess != random_num
  34. #add 1 to number of guesses for every guess
  35. number_guesses += 1
  36. #if user guess is incorrect, tell user higher or lower
  37. if user_guess > random_num
  38. print "Guess lower: "
  39. user_guess = gets.chomp.to_i
  40. else print "Guess higher: "
  41. user_guess = gets.chomp.to_i
  42. end
  43. end
  44. #when guess correctly, print winning message with total of guesses made
  45. puts "You guessed it! It only took " + number_guesses.to_s + " guesses!"
  46. puts
  47.  
  48. #2 --- Write a program that plays duck duck goose. Allow the user to enter the player's number they want to call goose on, and then say "duck" for each player before the "goose", then say "goose" for the chosen player.
  49. starting_player = 1
  50.  
  51. #welcome and ask which player is goose
  52. puts "Welcome to Duck, Duck, Goose. Which player do you want Goose to land on?"
  53. print "Goose is player number: "
  54. goose = gets.chomp.to_i
  55.  
  56. #begin duck, duck, goose
  57. for starting_player in 1..goose
  58. if starting_player === goose
  59. puts "Player " + starting_player.to_s + ": GOOSE"
  60. else puts "Player " + starting_player.to_s + ": Duck"
  61. end
  62. end
  63. puts
  64.  
  65. #2 --- Write a program that allows a user to enter the number of petals on a flower. Then one by one, print “plucking petal #1: they love me!”. Alternate “They love me” and “They love me not” as well as increase the petal number for each petal.
  66.  
  67. # user input on how many petals in a flower
  68. puts "Welcome to Petal Plucker! How many petals do you want in your flower?"
  69. print "Number of petals: "
  70. petals = gets.chomp.to_i
  71.  
  72. for i in 1..petals
  73. # if petal is even, print they love me not
  74. if i % 2 === 0
  75. puts "Plucking petal #" + i.to_s + ": they love me not"
  76. # if petal is odd, print they love me
  77. else puts "Plucking petal #" + i.to_s + ": they love me"
  78. end
  79. end
  80. puts
  81.  
  82. #4 --- You don't trust your users. Modify the program below to require the user to enter the same value twice in order to add that value to the total.
  83.  
  84. puts "Hello! We are going to total some numbers! Enter a number twice to confirm you want that number totaled."
  85. puts "Enter a negative number to quit."
  86.  
  87. #array to add numbers
  88. number_list = []
  89. previous_value = nil
  90. exit = false
  91. total = 0
  92.  
  93. while exit === false
  94. input = gets.chomp.to_i
  95. #iniating calculating and storing first value
  96. if previous_value === nil
  97. previous_value = input
  98. #check if new input is same as previous_value
  99. elsif input === previous_value && input >= 0
  100. total += input
  101. previous_value = nil
  102. #if input is same as previous_value, exit program
  103. elsif previous_value === input && input < 0
  104. exit = true
  105. #values do not match
  106. else previous_value = input
  107. end
  108. end
  109.  
  110. puts "Result: #{total}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement