2016-12-31 16 views
-3
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') 


# Cost of flying to a city. This code is verified in Jupyter! It works. 


def hotel_cost(nights): 
    return 140*nights 


# Cost of staying in a hotel. This code is verified in Jupyter! It works. 


def rental_car_cost(days): 
    if days<3: 
     cost = 40*days 
    if days>=7: 
     cost = 40*days - 50 # Discount 
    elif days>=3: 
     cost = 40*days - 20 # Discount 
    return cost 

# cost of renting a car. 

def trip_cost(city, days, spending_money): 
    return rental_car_cost(days) + plane_ride_cost('city') + hotel_cost(days) 
#total cost 

다음 오류가 발생합니다.코드 실행 중 오류가 발생했습니다.

trip_cost('Tampa', 0, 0) raised an error: maximum recursion depth exceeded in cmp 

이제 Jupyter에서 각 코드를 개별적으로 실행 했으므로 잘 작동합니다. 그러나 하나의 코드가 아닙니다.

답변

3

당신은 자체 내에서이 함수를 호출 :

def plane_ride_cost(city): 
    if city== 'Charlotte' : 
    .... 
    plane_ride_cost('city') 

이 무한 재귀입니다. 파이썬 인터프리터는 다행히 그 전에 중지하고 재귀 예외를 발생시킵니다.

은 아마 당신은 어쨌든 어떻게해야 있었는지 그 라인을 (제거해야 해결하기 위해? 'city'가 유효한 city 없습니다.

관련 문제