2015-04-01 57 views
0

编译错误后:不能调用功能正常的蟒蛇由于未知的原因

Traceback (most recent call last): 
    File "python", line 29, in <module> 
    File "python", line 26, in trip_cost 
    TypeError: 'int' object is not callable 

以下是我写的开支计算应用程序的代码。最后在trip_cost函数内传递四个参数,但在函数定义中定义了四个参数。

def hotel_cost(nights): 
    return 140 * nights 
def spending_money(money): 
    return money 
def plane_ride_cost(city): 
    if city == "Charlotte": 
     return 183 
    elif city == "Tampa": 
     return 220 
    elif city == "Pittsburgh": 
     return 222 
    elif city == "LosAngeles": 
     return 475 
def rental_car_cost(days): 
    cost = 40 
    if days >= 7: 
     return (cost * days - 50) 
    elif days >= 3 < 7: 
     return (cost * days - 20) 
    elif days < 3: 
     return (cost * days) 
def trip_cost(city,days,spending_money): 
    total_trip_cost = plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days) + spending_money(spending_money) 
    return total_trip_cost 

print trip_cost("LosAngeles",5,600) 

回答

2

局部变量spending_money过度写在你的trip_cost功能的范围功能spending_money()

由于spending_money()函数没有做任何事情,所以您可以直接添加它。

+0

然后它说:trip_cost应该采取三个参数:city,days和spend_money(按该顺序)。其已经以相同的顺序排列。 – kashminder 2015-04-01 09:36:28

+0

你有什么改变? – TZHX 2015-04-01 09:39:46

+0

我已将函数名称spend_money更改为money并将其更改为spend_money。获得1995年旅行成本与上述错误。 – kashminder 2015-04-01 09:44:50

0
def trip_cost(city,days,spending_money): 
total_trip_cost = plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days) + spending_money(spending_money) 
return total_trip_cost 
在这部分代码

,您所要求的功能spending_money(spending_money),问题是变量和函数都是一样的命名,所以Python asumes您呼叫的函数内部的函数? 为什么人类会这样对我,我感到困惑,说python

一个很好的提示和解决方案是更改变量名称或函数名称。

尝试:

def trip_cost(city,days,travelling_cash): 
total_trip_cost = plane_ride_cost(city) + rental_car_cost(days) +hotel_cost(days) + spending_money(travelling_cash) 
return total_trip_cost 

队友的欢呼声!