为什么它说我已达到最大递归深度?我该如何解决它?

问题描述 投票:0回答:1
cities = ["Boston", "Las Vegas", "Porto Alegre", "Cape Town", "Berlin", "Okinawa", "Sydney"]
hotels = [255, 450, 150, 250, 650, 675, 850]
flights = [950, 3000, 4500, 2750, 2500, 3500, 10500]


def add_destination(city, hotel, flight):
    global cities, flights, hotels
    print("""Add your desired destination. Please include your hotel and flight cost,
    as well as the name of your desired location and hotel.""")
    new_place_city = input("Please type your new city: ")
    new_place_hotel = int(input("Please type your new hotel's price per night: "))
    new_place_flight = int(input("Please type the cost of your flight:"))
    cities.append(new_place_city)
    hotels.append(new_place_hotel)
    flights.append(new_place_flight)

    cost = [flights] + [hotels]

def calculate_total_cost(hotels, flights):
    global city, hotel, flight, cost
    f_cost = sum(flights)
    h_cost = sum(hotels)
    t_cost = sum({h_cost, f_cost})
    result = calculate_total_cost(hotels, flights)

   


def display_budget_summary(cities, hotels, flights):
    for i in range(len(cities)):
        print(f"{cities[i]}, ${hotels[i]} ${flights[i]}.")
    result = calculate_total_cost(hotels, flights)
    print(f"Your current budget/money spent is ${result}.")

while True:
    print(f"""Currently, the cities you plan to visit during your vacation are {cities}."
    Please choose an option to make any edits to your vacation.""")
    print("1. Add a destination")
    print("2. View budget summary")
    print("3. Exit program")

    option = int(input("Please choose an answer: "))

    if option == 1:
        add_destination(cities, hotels, flights)

    elif option == 2:
        display_budget_summary(cities, hotels, flights)

    elif option == 3:
        break

    else:
        print("Please choose a valid option.")

上面提供的是我的代码。当我在 Online Python 上运行此程序时,我收到错误消息,指出我已达到最大递归距离,并且我对如何修复此问题感到困惑。据我了解,大部分问题都集中在显示功能上。谁能详细说明我如何修复这个错误?

python
1个回答
0
投票

这个函数就是问题所在:

def calculate_total_cost(hotels, flights):
    global city, hotel, flight, cost
    f_cost = sum(flights)
    h_cost = sum(hotels)
    t_cost = sum({h_cost, f_cost})
    result = calculate_total_cost(hotels, flights)

函数调用自身,函数调用自身,函数调用自身……永远。

© www.soinside.com 2019 - 2024. All rights reserved.