Skip to content

Instantly share code, notes, and snippets.

@nmajor
Created July 2, 2018 16:16
Show Gist options
  • Save nmajor/494f60e1f6624f5579cbf1af4f360556 to your computer and use it in GitHub Desktop.
Save nmajor/494f60e1f6624f5579cbf1af4f360556 to your computer and use it in GitHub Desktop.
# Puts a welcome message
puts 'WELCOME TO THE SUPERSTORE!'
# Define a sum of 0 euro
sum = 0
# Define a hash (of shopping items)
# with keys as STRINGS and values as floats
items = {
'kiwi' => 1.25,
'bacalhau' => 9.0,
'laranja' => 0.6,
'cafe' => 0.55,
}
# puts each key and its value (hash.each)
items.each do |key, value|
puts "#{key}: $#{value}"
end
# LOOP START
user_choice = nil
until user_choice == 'quit'
# Ask user for item to add
puts "Please add an item (or 'quit' to checkout)"
# gets.chomp the user choice (store in a variable)
user_choice = gets.chomp
break if user_choice == 'quit'
# check if user choice exists in hash
if items.key?(user_choice)
sum = sum + items[user_choice]
puts "New balance: #{sum}"
else
puts "sorry we dont have #{user_choice}"
end
end
puts "Thank you! Your balance is #{sum}"
# if choice exists - Add value of choice to sum
# if choice doesnt exist - puts "sorry we dont have #{user_choice}"
# if choice is "quit" - END LOOP - Print the total bill
# ------------------ Horses! ---------------------------
# RULES: All code should go in a method
# RULES: Always indent!
# Array with the horse names
# Get user's choice [index]
# puts the horse names with their index
# Generate random horse index for winner
# Compare user choice to winner index
# Puts out winner name
# Puts out user result (if user won or not)
def play
horses = [
"Seabiscuit",
"Cisco",
"Stewbal",
"Spear of the Conqueror",
"Vlad the horse impaler",
]
horses.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
puts "Which horse do you pick?"
user_choice = gets.chomp.to_i - 1
winner = rand(0..horses.size)
if user_choice == winner
puts "You WIIIIIIIIN GOOOOOOOAAAAAL!, #{horses[winner]} was the winner!"
else
puts "You Lose! #{horses[winner]} was the winner"
end
end
play
# ------------------ Calculator! ---------------------------
require_relative "calculator"
# PSUDO CODE
# Use some sort of while or until loop
# Define a condition for the loop
# Put everything inside the loop
# Ask the user if they want to leave in each loop
# Change condition of loop if ^ yes
# Print goodbye message if they leave
def interface
continue_input = 'y'
while continue_input == 'y'
puts "Enter first number > "
first_number = gets.chomp.to_i
puts "Enter first number > "
second_number = gets.chomp.to_i
puts "Choose operation [+][-][*][/] > "
user_operation = gets.chomp
result = calculate(first_number, second_number, user_operation)
puts "Result: #{result}"
while continue_input == 'y' || continue_input == 'n'
puts "Continue? y/n >"
continue_input = gets.chomp
end
end
end
interface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment