简体   繁体   English

如何让我的 Python 列表接受用户输入创建的每个字典?

[英]How to get my Python list to accept each dictionary created by user input?

#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 category 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.每次我的代码循环时,它都会更新字典,但不是存储初始字典,而是我的列表将用新输入替换以前的输入 (dict)。 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().编辑:我实际上复制了我有budget.update()而不是budget.append()的wring版本。

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

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

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