2016-12-31 19 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 

Es wird folgender Fehler angezeigt.Fehler beim Ausführen eines Codes

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

Jetzt habe ich jeden Code einzeln in Jupyter laufen und es ist gut zu arbeiten. Aber nicht als ein Code.

Antwort

3

Sie rufen diese Funktion in sich:

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

dies ist eine unendliche Rekursion. Der Python-Interpreter stoppt glücklicherweise davor und löst eine Rekursionsausnahme aus.

beheben Sie wahrscheinlich diese Zeile entfernen müssen (was es ohnehin tun sollte? 'city' keine gültige city ist.

Verwandte Themen