缺少1个必需的位置参数python

时间:2017-03-26 02:17:19

标签: python arguments

我对编程很陌生,而且解决方案可能很简单,但如果有人能够解释发生了什么,那就意味着很多:)

def loan (loan_amount,number_of_weeks):
    return (loan_amount/number_of_weeks)

loan_amount= int(input("Enter an amount: "))

number_of_weeks= int(input("Enter a number of weeks: "))


loan(loan_amount/number_of_weeks)

print ("you must repay",loan,"per week to repay a loan of",loan_amount,"in",number_of_weeks,"weeks")

错误代码

Enter an amount: 5
Enter a number of weeks: 5
Traceback (most recent call last):
  File "C:/Users/Ethan/PycharmProjects/untitled1/Loan.py", line 7, in <module>
    loan(loan_amount/number_of_weeks)
TypeError: loan() missing 1 required positional argument: 'number_of_weeks'

2 个答案:

答案 0 :(得分:2)

您定义了贷款以获取两个参数。所以,你必须称之为:

loan(loan_amount, number_of_weeks)

抬起头,您可能希望将结果分配给稍后打印的变量。打印loan打印功能对象表示。

答案 1 :(得分:0)

是的,在你对loan()的调用中,你有一个斜线,你应该有一个逗号。

loan(loan_amount/number_of_weeks)

应该是

loan(loan_amount,number_of_weeks)

另外,在下一行中,您打印&#34;贷款&#34;,但是没有设置任何内容。您可以将贷款输出设置为变量:

loan_var = loan(loan_amount/number_of_weeks)
print ("you must repay",loan_var,"per week to repay a loan of",loan_amount,"in",number_of_weeks,"weeks")

或直接在print语句中调用它:

print ("you must repay",loan(loan_amount/number_of_weeks),"per week to repay a loan of",loan_amount,"in",number_of_weeks,"weeks")