Created
July 12, 2019 15:36
-
-
Save Martin-Alexander/60cdde0463d48f0dd2f2b46a5ee9d134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Say hello to the user | |
# Prompt the user for a first number | |
# Get user input | |
# Prompt the user for a second number | |
# Get user input | |
# Prompt the user for an operator | |
# Run the appropraite calculation on the two numbers | |
# Print the result | |
puts "Welcome to the calculator app" | |
puts "First number:" | |
first_number = gets.chomp.to_i | |
puts "Second number:" | |
second_number = gets.chomp.to_i | |
puts "Operation:" | |
operation = gets.chomp | |
if operation == "+" | |
result = first_number + second_number | |
elsif operation == "-" | |
result = first_number - second_number | |
elsif operation == "/" | |
result = first_number.fdiv(second_number) | |
elsif operation == "*" | |
result = first_number * second_number | |
end | |
puts result | |
# Start until loop | |
# Ask for inputs and calculate | |
# Ask if the user would like to continue | |
# Stop the loop if they are done | |
puts "Welcome to the calculator app" | |
continue = nil | |
until continue == "n" | |
# CODE FROM PREVIOUS SECTION | |
puts "Would you like to continue [y/n]:" | |
continue = gets.chomp | |
end | |
puts "Goodbye" | |
# Refactor calculating code into seperate method | |
# Inputs (arguments): first number, second number, and operation | |
# Output: number | |
# Place code in 'calculator.rb' | |
# Use `require_relative` at the top of 'interface.rb' | |
# Integrate this method into your interface code | |
def calculate(first_number, second_number, operation) | |
if operation == "+" | |
result = first_number + second_number | |
elsif operation == "-" | |
result = first_number - second_number | |
elsif operation == "/" | |
result = first_number.fdiv(second_number) | |
elsif operation == "*" | |
result = first_number * second_number | |
end | |
return result | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define and store all the horses (array of strings) | |
# Loop over and print out all horse names | |
# Ask the user to bet on a horse | |
# Get user input | |
# Randomly pick a winning horse | |
# Tell the user if their horse won | |
horses = ["horse_1", "horse_2", "horse_3", "horse_4"] | |
horses.each do |horse| | |
puts horse | |
end | |
puts "Place your bet" | |
user_bet = gets.chomp | |
winning_horse = horses.sample | |
puts "#{winning_horse} won!" | |
if winning_horse == user_bet | |
puts "Congratulations" | |
else | |
puts "You lost your bet" | |
end | |
# Start player off with a balance of money | |
# Print horses names | |
# Begin loop | |
# Ask for user bet | |
# Print results of race | |
# If the user lost substract from balance | |
# If the user won add to balance | |
# Print out the user's balance | |
# Ask the user if they would like to continue betting | |
horses = ["horse_1", "horse_2", "horse_3", "horse_4"] | |
balance = 100 | |
horses.each do |horse| | |
puts horse | |
end | |
continue = nil | |
until continue == "n" | |
puts "Place your bet" | |
user_bet = gets.chomp | |
winning_horse = horses.sample | |
puts "#{winning_horse} won!" | |
if winning_horse == user_bet | |
puts "Congratulations" | |
balance += 50 | |
else | |
puts "You lost your bet" | |
balance -= 10 | |
end | |
puts "Your balance is #{balance}" | |
puts "Would you like to continue [y/n]:" | |
continue = gets.chomp | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Data: | |
# Represent the items on sale with a hash | |
# Item names as KEYS and thier prices as VALUES | |
# Represent the users cart as an array of item names STRINGS | |
# Welcome the user to the store | |
# Loop through all the items on sale and print out their name and price | |
# Start a loop | |
# Ask the user what they would like to buy | |
# If you have it, store it in thier cart | |
# Ask the user if they want to continue shopping | |
# Once they're done, loop through the user's cart and calculate the total price | |
store = { | |
"kiwi" => 1.25, | |
"banana" => 0.5, | |
"mango" => 4, | |
"asparagus" => 9 | |
} | |
shopping_cart = [] | |
puts "WELCOME!" | |
store.each do |item, price| | |
puts "#{item}: #{price}" | |
end | |
user_choice = nil | |
until user_choice == "quit" | |
puts "What would you like to buy? ('quit' to checkout)" | |
user_choice = gets.chomp | |
if store.key?(user_choice) | |
shopping_cart << user_choice | |
end | |
end | |
total = 0 | |
shopping_cart.each do |item| | |
total += store[item] | |
end | |
puts "Total price is #{total}" | |
# When the user makes a choice, ask them how many items they would like | |
# Fill up the cart with that many items | |
store = { | |
"kiwi" => 1.25, | |
"banana" => 0.5, | |
"mango" => 4, | |
"asparagus" => 9 | |
} | |
shopping_cart = [] | |
puts "WELCOME!" | |
store.each do |item, price| | |
puts "#{item}: #{price}" | |
end | |
user_choice = nil | |
until user_choice == "quit" | |
puts "What would you like to buy? ('quit' to checkout)" | |
user_choice = gets.chomp | |
if store.key?(user_choice) | |
puts "How many?" | |
amount = gets.chomp.to_i | |
amount.times do | |
shopping_cart << user_choice | |
end | |
end | |
end | |
total = 0 | |
shopping_cart.each do |item| | |
total += store[item] | |
end | |
puts "Total price is #{total}" | |
# Modify the store data: | |
# Hash with item names (stings) as KEYS, and hashes as VALUES | |
# Those hashes will have to key/value pairs, one for price and the other for quantity | |
# Update existing code to respect this new format (printing loop & total price calculation) | |
# When the user enters their desired quantity only add to the card if there is enough quantity | |
# Update the hash to reflect the new quantity of that item | |
store = { | |
"kiwi" => { "price" => 1.25, "quantity" => 10 }, | |
"banana" => { "price" => 0.5, "quantity" => 10 }, | |
"mango" => { "price" => 4, "quantity" => 10 }, | |
"asparagus" => { "price" => 9, "quantity" => 10 } | |
} | |
shopping_cart = [] | |
puts "WELCOME!" | |
store.each do |item, details| | |
price = details["price"] | |
quantity = details["quantity"] | |
puts "#{item}: #{price} (#{quantity} available)" | |
end | |
user_choice = nil | |
until user_choice == "quit" | |
puts "What would you like to buy? ('quit' to checkout)" | |
user_choice = gets.chomp | |
if store.key?(user_choice) | |
puts "How many?" | |
amount = gets.chomp.to_i | |
if store[user_choice]["quantity"] >= amount | |
amount.times do | |
shopping_cart << user_choice | |
end | |
store[user_choice]["quantity"] -= amount | |
else | |
puts "Sorry, there are only #{store[user_choice]["quantity"]} left" | |
end | |
end | |
end | |
total = 0 | |
shopping_cart.each do |item| | |
total += store[item]["price"] | |
end | |
puts "Total price is #{total}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment