Skip to content

Instantly share code, notes, and snippets.

@Haumer
Created July 13, 2021 16:54
Show Gist options
  • Save Haumer/c08ddccb273ad8be48bedba1e4c49042 to your computer and use it in GitHub Desktop.
Save Haumer/c08ddccb273ad8be48bedba1e4c49042 to your computer and use it in GitHub Desktop.
puts "----------------------------------------"
puts "-----Welcome to The Price is Right!-----"
puts "----------------------------------------"
# 1) make a method
# 2) computer has to pick a random number (save it to a variable)
# 3) prompt the user to also pick/guess a number (save it to a variable)
def price_is_right
computer_guess = rand(1..100)
puts "Guess the price of this wonderful imaginary product"
human_guess = gets.chomp.to_i
# 4) create a 'guess' counter variable to keep track of our attempts
# 5) create a loop while/until
# (condition of the loop is user_guess == pc_guess)
guess_count = 1
until human_guess == computer_guess
# 6) create an if statment to check if the computer guess is higher or lower
# 7) get the new user guess if inside the loop
if human_guess > computer_guess
puts "lower"
else
puts "higher"
end
# puts human_guess > computer_guess ? "Lower" : "Higher"
# condition ? true : false
human_guess = gets.chomp.to_i
# 8) increment guess_count
guess_count += 1
end
# 9) display a nice ending message
puts "Congrats the price was #{computer_guess} and it took you #{guess_count} attempts."
end
price_is_right
# 1) we need to require the date object
require "date"
# 2) create a method to calculate the days until chrismas
def days_until_xmas
# 3) set christmas as a date (25. december) in a variable
xmas = Date.new(2021, 12, 25)
# 4) set todays date in a variable
today = Date.today
# today = Date.new(2021, 12, 27)
# 5) subtract today from christmas
days = xmas - today
# 7) if our variable 'days' is negative, add 365 to days
if days.negative?
days.to_i + 365
else
days.to_i
end
end
puts days_until_xmas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment