Created
January 27, 2024 20:39
-
-
Save L0NDONER/4e63f9134fa3d36440087b9baaa1e4eb 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
# 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