简体   繁体   中英

Missing 1 required positional arguments python

I'm quite new to programming, and the solution is probably easy, but if anybody could explain whats going on, it would mean heaps :)

code :

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

error code :

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'

You defined loan to take two arguments. So, you would have to call it like:

loan(loan_amount, number_of_weeks)

Heads up, you probably want to assign the result of that to a variable which you then later print. Printing loan prints the function object representation.

yup, in your call to loan() you have a slash where you should have a comma.

loan(loan_amount/number_of_weeks)

should be

loan(loan_amount,number_of_weeks)

also, in the next line, you print "loan", but that's not set to anything. You can either set the output of loan to a variable:

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

or call it directly in the print statement:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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