Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 43 additions & 30 deletions tripcalculator.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
def hotelCost(days):
return 140*days
from random import randint

def planeRideCost(city):
# Instead of using a conditional chain for this, can you
# rewrite it using a Python dictionary data structure?
if city == "Charlotte":
return 183
if city == "Tampa":
return 220
if city == "Pittsburgh":
return 222
if city == "Los Angeles":
return 475
class tripCostCalculator():

def __init__(self, city: str, days: int, spending_money: int):
self.city = city.title()
self.days = days
self.spending_money = spending_money

def hotelCost(self) -> int:
return int(140 * self.days)

def planeRideCost(self) -> int:
try:
planeride_costs = {
"Charlotte": 183,
"Tampa": 220,
"Pittsburgh": 222,
"Angeles": 475,
}
return planeride_costs[self.city]
except KeyError:
return randint(120, 999)

def rentalCarCost(self) -> int:
cost = self.days * 40
if self.days >= 7:
cost -= 50
elif self.days >= 3:
cost -= 20
return int(cost)

def calculate_tripCost(self):
return self.spending_money + self.rentalCarCost() + self.hotelCost() + self.planeRideCost()

def rentalCarCost(days):
cost = days*40
if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost

# Your indentation levels above differ from the level below.
# While the code will still work, that's bad form. Pick a level and
# stick to it across all function internals, etc. I suggest either 2
# or 4 spaces, using actual spaces not TAB characters.

def tripCost(city,days, spendingMoney):
# The change above will have to be adjusted for here of course.
return spendingMoney + rentalCarCost(days) + hotelCost(days) + planeRideCost(city)

print tripCost("Pittsburgh",7,600)
if __name__ == "__main__":
city = "Tampa"
days = 6
spending_money = 600
print("Spending money for the trip", tripCostCalculator(city, days, spending_money).calculate_tripCost())

city = "Atlanta"
days = 6
spending_money = 600
print("Spending money for the trip", tripCostCalculator(city, days, spending_money).calculate_tripCost())