[英]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)
每次我的代碼循環時,它都會更新字典,但不是存儲初始字典,而是我的列表將用新輸入替換以前的輸入 (dict)。 我還附上了一些結果。 請幫忙! 代碼片段
編輯:我實際上復制了我有budget.update()而不是budget.append()的wring版本。
這是更正后的代碼。 您只能附加到列表。
此外,當您更新字典時,舊值也會更新。 因此,您每次都需要創建新詞典並將其推送到列表中。
#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.