def hotel(days):
return days*50
def plane_ticket(city):
city = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico" :100}
return city
def car(days):
rent=days*10
if days >= 7:
return rent -5
elif days >= 5:
return rent -1
else:
return rent
def trip_cost(city, days, extras):
return sum([hotel(days),plane_ticket(city),car(days),extras])
print (trip_cost("Chicago",4,300))
我希望分配给字典中字符串的数字在总和中用作整数
答案 0 :(得分:3)
非常简单:
def plane_ticket(city):
cities = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico" :100}
return cities[city]
答案 1 :(得分:3)
您遇到此问题,因为您的plane_ticket
功能并未返回您的想法。
def plane_ticket(city):
city = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico" :100}
return city
此函数返回整个字典city
,而不是单个值。
你需要做这样的事情:
def plane_ticket(city):
city_prices = {"Chicago": 180, "Boston": 170, "Orlando": 160,"Ciudad de Mexico" :100}
return city_prices[city]
您可能希望在此处添加一些错误检查。如果函数通过了字典中不存在的城市,那么您将收到错误。
进行上述更改后,打印出值720