2016-07-28 103 views
1

所以我真的很陌生(3天),我在代碼學院,我爲這些活動之一編寫了代碼,但是當我運行它時,它顯示最大遞歸深度錯誤,我正在運行它在代碼學院的python控制檯中,同時在我自己的ipython控制檯上。頁面上的提示是沒有用的,任何人都可以解釋如何解決這個問題嗎? 感謝最大遞歸深度錯誤?

def hotel_cost(nights): 
    return (nights * 140) 

def plane_ride_cost(city): 
    if plane_ride_cost("Charlotte"): 
     return (183) 
    if plane_ride_cost("Tampa"): 
     return (220) 
    if plane_ride_cost("Pittsburgh"): 
     return (222) 
    if plane_ride_cost("Loas Angeles"): 
     return (475) 

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): 
    return hotel_cost(nights) + plane_ride_cost(city) + rental_car_cost(days) 
+0

*編輯*我知道的拼寫錯誤的:') –

回答

1

可能:

def plane_ride_cost(city): 
    if city == "Charlotte": 
     return (183) 
    if city == "Tampa": 
     return (220) 
    if city == "Pittsburgh": 
     return (222) 
    if city == "Los Angeles": 
     return (475) 

錯誤是:

在每個遞歸步驟plane_ride_cost(city)稱爲plane_ride_cost("Charlotte")

不是最好的,但更好的方法:

def hotel_cost(nights): 
    return nights * 140 

plane_cost = { 
    'Charlotte' : 183, 
    'Tampa' : 220, 
    'Pittsburgh' : 222, 
    'Los Angeles' : 475, 
} 

def plane_ride_cost(city): 
    if city not in plane_cost: 
     raise Exception('City "%s" not registered.' % city) 
    else: 
     return plane_cost[city] 

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): 
    return hotel_cost(nights) + plane_ride_cost(city) + rental_car_cost(days) 
+0

更好地使用'dict',如果'城市引發'Exception' '不要在字典中。 –