我想申請什麼我已經瞭解的功能和參數,所以我想出了可以計算的學費(純屬假設)我的函數和參數代碼有什麼問題?
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)
有人可以解釋,也許修復代碼,所以我可以分析什麼是錯的?
爲什麼'laptop'的功能位置參數'renting_1'?它不應該在那裏嗎?因爲你正在函數中重新定義'laptop'。 – abccd
你在這裏給出了一個參數......'renting_1(weeks)'閱讀錯誤。 –
對於您的函數'renting_1','renting_2'和'school_cost',您添加了第一個參數,該參數不應該在那裏。從參數中分別刪除'laptop','textbooks'和'cost'。這可能是一個錯字,或者你不太理解論證如何工作。還要閱讀錯誤信息,那個很清楚。 –