2016-06-22 225 views
0
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) 

I輸入上面的代碼,並獲得各種錯誤,其中之一是:無法解決碼錯誤

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

@Sayse通過編輯關於Code Academy的部分,您已經弄清楚錯誤的「糟糕,再試一次」部分來自哪裏。 –

+0

@JonathonReinhart - 在這種情況下,應該刪除「糟糕,再試一次」。 – Sayse

+0

@Sayse你讓事情變得更糟。這不是標準的Python異常消息。 –

回答

2

該錯誤是從該行提出:

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

您正在調用功能plane_ride_cost,其中變量n未在範圍中定義。

1

trip_cost,你打電話

plane_ride_cost(n) 

n是沒有定義。你的意思是city

+0

比'天'更像'城市' – Dschoni

+0

@Dschoni當然,謝謝。編輯。 –