Home > database >  How to get my Python list to accept each dictionary created by user input?
How to get my Python list to accept each dictionary created by user input?

Time:06-11

#line used to accept user's net pay
net_income = float(input("Please enter your net income: "))

#line used to query if user wants to input budgeted line item
add_exp_item= input("Do you want to add an expenditure item (yes or no): ").lower()

#intialization of dictionary and list used to store user input 
budget = []
budget_listItem = {}

#loop used to continuously query for budgeted expense item
while True:
    #line used to store description of expense
    d= input("Enter name description of expense: ")
    
    #line used to store numerical value of expense
    a = float(input("Enter cost/budgeted amount: "))
   
    #line used to store the type of expense 
    t = input("Enter catagory type that such as whether it is a want, need, saving: ")
    
    #line used to store all user input in dictionary 
    budget_listItem.update({'description': d, 'amount': a, 'type': t})
    
    #testing line to to see what is stored in dictionary
    print(budget_listItem)

    #line used to store each expense line (each dictionary) in a list
    budget.append(budget_listItem)
  
    #testing line to check what is in list of dictionaries
    print(budget)
    
    #line used to query if user wants to enter another line item
    add_exp_item= input("Do you want to add an expenditure item (yes or no): ").lower()
    print("*************************************************** \n")

    #bool used to break out of loop if user no longer wants to enter line items
    if add_exp_item == "no":
        break

print("\n \n")

#final print of list of dictionaries
print(budget)

Each time my code would loop it will update the dictionary but instead of storing the initialdictionary, my list will replace the previous input (dict) with the new input. I have also attach a snip of results. Please help! Code Snip

Edit: I had actually copied the wring version where i had budget.update() instead of budget.append().

CodePudding user response:

Here is the corrected code. You can only append to a list. Also when you update a dictionary, the old values will get updated. Hence you need to create new dictionaries each time and push it into the list.

#line used to accept user's net pay
net_income = float(input("Please enter your net income: "))

#line used to query if user wants to input budgeted line item
add_exp_item= input("Do you want to add an expenditure item (yes or no): ").lower()

#intialization of list used to store user input 
budget = []

#loop used to continuously query for budgeted expense item
while True:
    #line used to store description of expense
    d= input("Enter name description of expense: ")
    
    #line used to store numerical value of expense
    a = float(input("Enter cost/budgeted amount: "))
   
    #line used to store the type of expense 
    t = input("Enter catagory type that such as whether it is a want, need, saving: ")
    
    #create new dictionary for each input
    budget_listItem = {'description': d, 'amount': a, 'type': t}
    
    #testing line to to see what is stored in dictionary
    print(budget_listItem)

    #line used to store each expense line (each dictionary) in a list
    budget.append(budget_listItem)
  
    #testing line to check what is in list of dictionaries
    print(budget)
    
    #line used to query if user wants to enter another line item
    add_exp_item= input("Do you want to add an expenditure item (yes or no): ").lower()
    print("*************************************************** \n")

    #bool used to break out of loop if user no longer wants to enter line items
    if add_exp_item == "no":
        break

print("\n \n")

#final print of list of dictionaries
print(budget)
  • Related