繁体   English   中英

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

[英]Missing 1 required positional arguments python

我对编程很陌生,解决方案可能很简单,但是如果有人可以解释发生了什么,那将意味着堆:)

代码

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'

您定义的借贷有两个参数。 因此,您必须这样称呼它:

loan(loan_amount, number_of_weeks)

请注意,您可能希望将结果分配给一个变量,然后在以后打印。 印刷loan印刷功能对象表示。

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

loan(loan_amount/number_of_weeks)

应该

loan(loan_amount,number_of_weeks)

另外,在下一行中,您将打印“贷款”,但这未设置为任何内容。 您可以将贷款的输出设置为变量:

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM