• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏吧

# 我的函数和参数代码有什么问题？

``````def renting_1(laptop, weeks):
laptop = 5 * weeks
if weeks > 10:
laptop -= 120
elif weeks > 5:
laptop -= 50
return laptop

def renting_2(textbooks, number_of_textbooks, weeks):
textbooks = number_of_textbooks * 20 + (10 * weeks)
if weeks >= 26:
textbooks -= (5 * (weeks - 26))
return textbooks

def school_cost(cost, weeks):
cost = 200 * weeks
return cost

def total_cost(weeks, number_of_textbooks):
return renting_1(weeks) + renting_2(number_of_textbooks, weeks) + school_cost(weeks)

print total_cost(22, 4)
``````

，当我运行它，我收到此

``````Traceback (most recent call last):
File "python", line 22, in <module>
File "python", line 20, in total_cost
TypeError: renting_1() takes exactly 2 arguments (1 given)
``````

===========解决方案如下：

``````def renting_1(weeks, laptop=None):
laptop = 5 * weeks
if weeks > 10:
laptop -= 120
elif weeks > 5:
laptop -= 50
return laptop

def renting_2(number_of_textbooks, weeks, textbooks=None):
textbooks = number_of_textbooks * 20 + (10 * weeks)
if weeks >= 26:
textbooks -= (5 * (weeks - 26))
return textbooks

def school_cost(weeks, cost=None):
cost = 200 * weeks
return cost

def total_cost(weeks, number_of_textbooks):
return renting_1(weeks) + renting_2(number_of_textbooks, weeks) + school_cost(weeks)

print total_cost(22, 4)
``````