Skip to content

Instantly share code, notes, and snippets.

@L0NDONER
Created January 27, 2024 20:39
Show Gist options
  • Save L0NDONER/4e63f9134fa3d36440087b9baaa1e4eb to your computer and use it in GitHub Desktop.
Save L0NDONER/4e63f9134fa3d36440087b9baaa1e4eb to your computer and use it in GitHub Desktop.
# Constants
GAS_CONSUMPTION_PER_HOUR = 20 # liters per hour
GAS_PRICE_PER_LITER = 3.12 # dollars
TICKET_SPEED_1 = 100 # km/h
TICKET_COST_1 = 200 # dollars
TICKET_SPEED_2 = 120 # km/h
TICKET_COST_2 = 800 # dollars
DISTANCES = [150, 450, 900] # km
def calculate_trip_cost(speed):
if speed <= 0:
return "Speed must be a positive number."
costs = []
for distance in DISTANCES:
time = distance / speed
gas_cost = GAS_CONSUMPTION_PER_HOUR * GAS_PRICE_PER_LITER * time
# Calculate tickets
ticket_cost = 0
if speed > TICKET_SPEED_2:
ticket_cost = (time // 2) * TICKET_COST_2
elif speed > TICKET_SPEED_1:
ticket_cost = (time // 2) * TICKET_COST_1
total_cost = gas_cost + ticket_cost
costs.append((time, total_cost))
return costs
# User input
try:
user_speed = float(input("Enter your planned travel speed (in km/h): "))
trip_costs = calculate_trip_cost(user_speed)
if isinstance(trip_costs, str):
print(trip_costs)
else:
for i, (time, cost) in enumerate(trip_costs):
print(f"{DISTANCES[i]}km - {time:.2f} hours for a cost of {cost:.0f} (including tickets)")
except ValueError:
print("Please enter a valid number for speed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment