Last active
July 2, 2018 13:03
-
-
Save ellijayne/c7757eb8f8d5075a244c73713333d292 to your computer and use it in GitHub Desktop.
Trip calculator
This file contains hidden or 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
# Calculate a trip time and cost given inputs for | |
# | |
# distance | |
# miles per gallon | |
# price per gallon | |
# speed in miles per hour | |
require 'rainbow' | |
def calc_option | |
puts "Trip Calculator" | |
puts "*" * 80 | |
puts Rainbow("[t] - Calculate Trip Time").aqua | |
puts Rainbow("[c] - Calculate Trip Cost").green | |
puts Rainbow("[q] - Quit Menu").red | |
print Rainbow("Please enter your choice: ").yellow | |
end | |
calc_option | |
calc_choice= gets.chomp.downcase | |
#calculate trip cost function | |
def trip_cost(distance, miles_per_gal, price_per_gal) | |
result = (((distance / 100) * miles_per_gal) * price_per_gal) | |
return result #also tried just typing 'result' here without the return.. | |
end | |
#conditional...... | |
until calc_choice == 'q' | |
case calc_choice | |
when "c" | |
print Rainbow("Please enter trip distance in miles: ").red | |
distance = gets.to_i | |
print Rainbow("Please enter how many miles per gallon of gas your car travels: ").red | |
miles_per_gal = gets.to_i | |
print Rainbow("Please enter the price you paid per gallon of gas: ").red | |
price_per_gal = gets.to_f | |
trip_cost(distance, miles_per_gal, price_per_gal) | |
puts Rainbow("The cost of your #{distance} mile trip, based on your car's fuel consumption of #{miles_per_gal} miles per gallon at a cost of $#{price_per_gal} is $#{result}.").aqua | |
calc_option | |
calc_choice = gets.chomp.downcase | |
end | |
end |
johnofsydney
commented
Jul 2, 2018
Hi EJ - see my changes on line 25: result = (((distance.to_f / 100) * miles_per_gal) * price_per_gal)
and line 42: puts Rainbow("The cost of your #{distance} mile trip, based on your car's fuel consumption of #{miles_per_gal} miles per gallon at a cost of $#{price_per_gal} is $#{trip_cost(distance, miles_per_gal, price_per_gal)}.").aqua
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment