I have completed the function but for some reason it is not returning loans <500 but rather all of them when I output list to csv file... it also returns none when i try to print the list...I know I am doing some thing wrong but not sure exactly what.
Here are the instructions...
- Create a new, empty list called
inexpensive_loans
. - Use a for loop to select each loan from a list of loans.
- Inside the for loop, write an if-statement to determine if the loan_price is less than or equal to 500
- If the loan_price is less than or equal to 500 then append that loan to the
inexpensive_loans
list. Print the list of inexpensive_loans.
loans = [
{
"loan_price": 700,
"remaining_months": 9,
"repayment_interval": "monthly",
"future_value": 1000,
},
{
"loan_price": 500,
"remaining_months": 13,
"repayment_interval": "bullet",
"future_value": 1000,
},
{
"loan_price": 200,
"remaining_months": 16,
"repayment_interval": "bullet",
"future_value": 1000,
},
{
"loan_price": 900,
"remaining_months": 16,
"repayment_interval": "bullet",
"future_value": 1000,
},
]
def inexpensive_loans(loan_price):
inexpensive_loans = []
for loan_price in loans.keys('loan_price'):
if loan_price <=500:
print(loan_price, loans [loan_price])
return inexpensive_loans
inexpensive_loans.append(loan_price[inexpensive_loans])
CodePudding user response:
Im editing my answer as I noticed some more things that will not work with your code.
- Name your function in a precise manner for example
get_inexpensive_loans
. That way it is clear what it does and how it is different from the variableinexpensive_loans
. - The parameter
loan_price
in your function definition in the first line is not needed, you get the individual loan prices from the list of loans. - As Junuxx pointed out,
loans
is an array not a dictionary, so there is nokeys
property and you don't need it. Usefor loan in loans
and access the price for each element by usingloan["loan_price"]
You may want to look at Python For Loops.
Original:
There are several things that are problematic with your piece of code, I'll go line by line.
- 2nd line: Why do you declare a variable with the same name as the function? I don't know about python but in other languages that can not work properly
- 6th line: Why do you return here? This would end your function at this point
- 7th line: You used your dictionary
inexpensive_loans
as an index toloan_price
but you need a number for thatloan_price
is a number, the line should read likeinexpensive_loans.append(loan_price)
CodePudding user response:
Your function has several confused bits to it, which I'll try to fix and describe in comments:
def inexpensive_loans(loan_list):
inexpensive_loans = [] # this variable is OK, but ideally you'd use a different name
for loan in loan_list: # you have a list of dicts, you can't directly get the prices
if loan["price"] <=500: # instead, get it here by indexing the dict
inexpensive_loans.append(loan) # append the dictionary we've iterated to
return inexpensive_loans # this needs to be unindented to happen outside the loop
print(inexpensive_loans(loans))
The instructions you show say to print the list, but I preserved good functional behavior by having the funciton return
the list it had made and put the print()
call outside the function where we call it. You could replace return inexpensive_loans
with print(inexpensive_loans)
if the function needs to do the printing directly (in that case, you wouldn't use print()
where you call the function).