Skip to content

Instantly share code, notes, and snippets.

@nmajor
Created July 2, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmajor/8604ab7cdd191415db8e8178ce976560 to your computer and use it in GitHub Desktop.
Save nmajor/8604ab7cdd191415db8e8178ce976560 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment