Skip to content

Instantly share code, notes, and snippets.

@ellijayne
Last active July 2, 2018 13:03
Show Gist options
  • Save ellijayne/c7757eb8f8d5075a244c73713333d292 to your computer and use it in GitHub Desktop.
Save ellijayne/c7757eb8f8d5075a244c73713333d292 to your computer and use it in GitHub Desktop.
Trip calculator
# 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
Copy link

# Calculate a trip time and cost given inputs for
#
# distance
# miles per gallon
# price per gallon
# speed in miles per hour
require 'rainbow'
require 'pry'

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.to_f / 100) * miles_per_gal) * price_per_gal)
  # NOTE I have added .to_f to distance so you dont get bogus results of 0
  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 $#{trip_cost(distance, miles_per_gal, price_per_gal)}.").aqua
    # You were using result as the last variable in this puts line.
    # But result is not a variable here. It lives inside trip_cost.

    calc_option
    calc_choice = gets.chomp.downcase
  end
end

@johnofsydney
Copy link

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