平等问题:
提议
把它看作是改进代码的一部分。我想是的,它没有回答你的问题。在
我不知道代码学院对这个练习有什么建议,但下面是一些更简单、更干净的方法:print "this code calculates the total price of a trip, using 4 functions"
def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
#So you can create dict and put for each city
#Key - name of city
#value - cost
CITY_COST = {
"Charlotte": 183,
"Pittsburgh" : 222,
"Los Angeles" : 475,
"Tampa": "220"
}
#Method from dict
#if city doesn't exists it'll return False
#The second param is default return if doesn't exist key into dict
#you can change if do you want
return CITY_COST.get(city, False)
def rental_car_cost(days):
cost = days * 40
if (days >= 7):
cost -= 50
elif(days >=3 ):
cost -=20
return cost
def trip_cost(city,days,spending_money):
return rental_car_cost(days)+hotel_cost(days)+ plane_ride_cost(city)+spending_money
city= raw_input("enter city name")
days= raw_input("enter number of days staying")
spending_money= raw_input("enter spendig money")
print trip_cost(city,days, spending_money)
关于Dict的文档