Home > Software design >  Iterating over a dictionary using for loop to return price < certain amount
Iterating over a dictionary using for loop to return price < certain amount

Time:12-05

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...

  1. Create a new, empty list called inexpensive_loans.
  2. Use a for loop to select each loan from a list of loans.
  3. Inside the for loop, write an if-statement to determine if the loan_price is less than or equal to 500
  4. 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 variable inexpensive_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 no keys property and you don't need it. Use for loan in loans and access the price for each element by using loan["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 to loan_price but you need a number for that
    loan_price is a number, the line should read like inexpensive_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).

  • Related