Skip to content

Instantly share code, notes, and snippets.

@chadmiller
Created March 26, 2020 01:44
Show Gist options
  • Save chadmiller/da14731624c1ededb34acb645efee6f6 to your computer and use it in GitHub Desktop.
Save chadmiller/da14731624c1ededb34acb645efee6f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from collections import namedtuple
Trip = namedtuple("Trip", "name expenses lodgingcost bagcost")
def prompt(message):
return input(message + " => ").strip()
def prompt_number(question):
while True:
try:
return float(prompt(question))
except ValueError:
print("That isn't a number.")
def get_trip():
destination = prompt("Leave empty for no answer. What destination for the trip?")
if not destination:
return None
return Trip(
destination,
prompt_number("What were yout transit expenses for your {!r} trip?".format(destination)),
prompt_number("What was the cost of lodging for your {!r} trip?".format(destination)),
50 * prompt_number("How many bags did you check for your trip to {!r}?".format(destination)))
trips = []
while True:
trip = get_trip()
if not trip:
break
trips.append(trip)
trips.sort(key=lambda t: (t.expenses+t.lodgingcost+t.bagcost), reverse=True)
total = sum(trip.expenses + trip.lodgingcost + trip.bagcost for trip in trips)
print("The total cost for all trips is: {:0.02f}".format(total))
for trip in trips:
trip_sum = trip.expenses + trip.lodgingcost + trip.bagcost
fraction = trip_sum / total
print()
print("The total cost for your {} trip is: {:0.02f}".format(trip.name, trip_sum))
print("Your trip to {} made up {}% of the total cost of all trips".format(trip.name, fraction*100))
if trips:
print("Your most expensive trip was {}".format(trips[0].name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment