无法解决代码错误

时间:2016-06-22 10:31:32

标签: python

def hotel_cost(days):
    return days*140

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

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

def trip_cost(city,days):
    return rental_car_cost(days)+ plane_ride_cost(n)+hotel_cost(days)

我输入上面的代码并得到各种错误,其中一个是:

trip_cost('Tampa', 5) raised an error: global name 'n' is not defined

2 个答案:

答案 0 :(得分:2)

此行引发错误:

return rental_car_cost(days)+ plane_ride_cost(n)+hotel_cost(days)

您正在使用未在范围中定义的变量plane_ride_cost调用函数n

答案 1 :(得分:1)

trip_cost,您正在调用

 plane_ride_cost(n)

但是n没有在那里定义。您的意思是city吗?