Skip to content

Instantly share code, notes, and snippets.

@georgekettle
Created January 19, 2021 01:02
Show Gist options
  • Save georgekettle/6ae74b3ccc2f8881ea2424e96c88cc30 to your computer and use it in GitHub Desktop.
Save georgekettle/6ae74b3ccc2f8881ea2424e96c88cc30 to your computer and use it in GitHub Desktop.
# TODO: you can build your calculator program here!
# create a calculator
require 'colorize'
def prompt(text)
puts text.yellow
end
def addition(first, last)
first + second
end
# welcome
puts "Welcome math buddy -- I am your friend"
# write prompt to the terminal
# puts "Enter your first number".yellow
prompt("Enter your first number")
# enter first number --> save to variable
first = gets.chomp.to_i
# write prompt to the terminal
prompt("Enter your second number")
# prompt(text)
# enter second number --> save to variable
second = gets.chomp.to_i
# ask the user for the operation
prompt("Enter the operator [+|-|*|/]")
# choose the operation --> save to variable
operation = gets.chomp
case operation
when "+" then answer = first + second
when "-" then answer = first - second
when "*" then answer = first * second
when "/" then answer = first.fdiv(second)
else
answer = "Wrong input, please try again"
end
puts "Your answer is #{answer}".green
# I accidentally deleted our livecode, so this is the LeWagon solution :)
# Define the horses
horses = ["Joe the Brave", "Black Stalion", "Ronny the Rat", "Tony Macaroni"]
# Define the initial user balance
balance = 100
# Display a welcome message
puts "Welcome to the horse race!"
puts "--------------------------"
# Start the loop
# Condition to loop: the user has at least 10 in his balance
while balance >= 10
# Display the names of the horses that are running, with a number starting from 1
puts "---------------------"
puts "Today our horses are:"
horses.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
# Ask the user which horse he wants to bet on
puts "Which horse do you want to bet on?"
print "> "
# Shift the user choice by -1 to get his horse index
user_horse_index = gets.chomp.to_i - 1
# Display the name of the horse that the user bet on
puts "You chose: #{horses[user_horse_index]}"
# Run the race: randomly select the winner horse index
winner_horse_index = rand(0...horses.length)
# Compute the result: compare the index of the horse of the user with the winner horse index
if user_horse_index == winner_horse_index
# If the user won, add 50 to his balance and display the winning message
balance += 50
puts "You won! Your balance is now #{balance}€"
else
# If the user lost, substract 10 to his balance and display the losing message
balance -= 10
puts "You lost! The winner was #{horses[winner_horse_index]} Your balance is now #{balance}€"
end
end
# Display an exit message
puts "-----------------------------------------"
puts "Thank you for playing! See you next time!"
require 'pry'
# display Welcome message
puts "--------------------"
puts "Welcome to Instacart"
puts "--------------------"
# define stock = An array of hashes (each item)
store_items = {
"kiwi": {price: 1.25, qty: 2},
"banana": {price: 1.25, qty: 5},
"mango": {price: 1.25, qty: 10},
"asparagus": {price: 1.25, qty: 4}
}
shopping_cart = Hash.new(0)
# eg format - {"kiwi": 1}
# so we can access like so shopping_cart["kiwi"]
answer = nil
puts "In our store today:"
store_items.each do |key, value|
puts "#{key} - $#{value[:price]} - #{value[:qty]} available"
end
puts "--------------------"
until answer == "exit"
puts "Which item? (or 'exit' to checkout)"
answer = gets.chomp
puts "How many would you like to buy?"
qty = gets.chomp
# byebug
if store_items[answer.to_sym][:qty] < qty.to_i
puts "there are not enough in the store"
elsif store_items[answer.to_sym]
shopping_cart[answer] += qty.to_i
store_items[answer.to_sym][:qty] -= qty.to_i
# update store
elsif answer == "exit"
else
puts "You have chosen an item not in our store"
end
p shopping_cart
puts ""
p store_items
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment