Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2011
    residenza
    bergamo
    Messaggi
    1,027

    [Python] funzioni raised an error

    buonasera ragazzi,
    ho scritto un pezzo di codice seguendo la guida codeacademy, sicuramente interpreto male il testo, ma non riesco a capire (è mezzoretta che guardo per capire) come mai ho questo errore:

    Oops, try again. hotel_cost(1) raised an error: maximum recursion depth exceeded
    il codice è questo

    codice:
    def hotel_cost(nights):
        nights = 1 * 140
        return hotel_cost(nights)
    def plane_ride_cost(city):
        if city == "bergamo":
            return 100
        elif city == "milano":
            return 140
        elif city == "brescia":
            return 105
        return plane_ride_cost(city)
    def rental_car_cost(days):
        cost = 40 * days
        if days >= 7:
            cost -= 50
        elif days >= 3:
            cost -= 20
        return cost
    def trip_cost(city, days):
        return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city)
    la consegna dell'esercizio invece è questa

    1. Below your existing code, define a function called trip_cost that takes two arguments, city and days.
    2. Like the example above, have your function return the sum of calling the rental_car_cost(days), hotel_cost(days), and plane_ride_cost(city) functions.

    It is completely valid to call the hotel_cost(nights) function with the variable days. Just like the example above where we call double(n) with the variable a, we pass the value of days to the new function in the argument nights.
    per eventuali chiarezze il codice ci esempio a cui riferisce il testo dell'esercizio è questo

    codice:
    def double(n):
        return 2 * n
    def triple(p):
        return 3 * p
    
    def add(a, b):
        return double(a) + triple(b)

  2. #2
    Utente di HTML.it L'avatar di Scara95
    Registrato dal
    Jul 2009
    residenza
    Zimella (VR)
    Messaggi
    2,589
    Magari hai già risolto, comunque:
    Concludi la funzione con una chiamata alla funzione stessa, non essendoci alcuna condizione per l'uscita la funzione continuerà a richiamarsi all'infinito.
    Lo stesso problema si presenta in plane_ride_cost se la città è sconosciuta.
    "Quid enim est, quod contra vim sine vi fieri possit?" - Cicerone, Ad Familiares

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.