Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.
- #class that checkes if values are numeric
- class String
- def is_number?
- true if Float(self) rescue false
- end
- end
- #random number in range 0-1000 with 0 included but not 1000
- random_num = rand(-1..1000)
- #FOR TESTING, REMOVE BEFORE SUBMITTING
- puts random_num
- number_guesses = 1
- #welcome and retrieve user's guess
- puts "Welcome to GUESS THAT NUMBER! Pick a number between 0 and 1000."
- print "Your guess: "
- user_guess = gets.chomp
- #method to check if grade is numeric
- if !user_guess.is_number?
- #if grade is not numeric, ask user for numeric value
- while !user_guess.is_number?
- print "Looks like you didn't enter a number. Re-enter a number: "
- user_guess = gets.chomp
- end
- end
- user_guess = user_guess.to_i
- #allow user to make guess until it's correct
- while user_guess != random_num
- #add 1 to number of guesses for every guess
- number_guesses += 1
- #if user guess is incorrect, tell user higher or lower
- if user_guess > random_num
- print "Guess lower: "
- user_guess = gets.chomp.to_i
- else print "Guess higher: "
- user_guess = gets.chomp.to_i
- end
- end
- #when guess correctly, print winning message with total of guesses made
- puts "You guessed it! It only took " + number_guesses.to_s + " guesses!"
- puts
- #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.
- starting_player = 1
- #welcome and ask which player is goose
- puts "Welcome to Duck, Duck, Goose. Which player do you want Goose to land on?"
- print "Goose is player number: "
- goose = gets.chomp.to_i
- #begin duck, duck, goose
- for starting_player in 1..goose
- if starting_player === goose
- puts "Player " + starting_player.to_s + ": GOOSE"
- else puts "Player " + starting_player.to_s + ": Duck"
- end
- end
- puts
- #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.
- # user input on how many petals in a flower
- puts "Welcome to Petal Plucker! How many petals do you want in your flower?"
- print "Number of petals: "
- petals = gets.chomp.to_i
- for i in 1..petals
- # if petal is even, print they love me not
- if i % 2 === 0
- puts "Plucking petal #" + i.to_s + ": they love me not"
- # if petal is odd, print they love me
- else puts "Plucking petal #" + i.to_s + ": they love me"
- end
- end
- puts
- #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.
- puts "Hello! We are going to total some numbers! Enter a number twice to confirm you want that number totaled."
- puts "Enter a negative number to quit."
- #array to add numbers
- number_list = []
- previous_value = nil
- exit = false
- total = 0
- while exit === false
- input = gets.chomp.to_i
- #iniating calculating and storing first value
- if previous_value === nil
- previous_value = input
- #check if new input is same as previous_value
- elsif input === previous_value && input >= 0
- total += input
- previous_value = nil
- #if input is same as previous_value, exit program
- elsif previous_value === input && input < 0
- exit = true
- #values do not match
- else previous_value = input
- end
- end
- puts "Result: #{total}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement