【发布时间】:2019-05-03 16:55:08
【问题描述】:
我现在有这个代码。
current_assets = {}
yes_list=["yes", "Yes", "y", "Y"]
while create_bs in (yes_list):
key=str(input("Enter a specific account.\nExamples: Checking Account
Balance, Investment in XYZ Corp., Parking Fine Payable, Note Payable,
Receviable From John Smith\n"))
value=float(input("Enter the value/historical cost of the asset or
liability.\n"))
account_type=str(input("What type of account is this?\nOptions: 1)
Current Assets 2) Noncurrent Assets 3) Current Liabilities 4) Noncurrent
Liabilities\n"))
if account_type in ("1", "Current Assets", "current assets", "CA"):
current_assets.update({key:value})
if key in current_assets:
current_assets[key].append({key:value})
尝试运行时出现两个问题:
-
我收到一个
AttributeError: 'float' object has no attribute 'append' -
该值似乎增加了两次。例如,而不是
{Cash: 100} and {Cash: 200} becoming {Cash: (100, 200)}, it becomes {Cash: 400}
【问题讨论】:
-
'current_assets`是什么数据结构?
-
如果
current_assets[key]是一个浮点值,你不能append另一个浮点数。相反,该值应该是一个列表,其中包含一个或多个浮点数。 -
@尼克字典
-
@JohnGordon 我修复了那个部分,它仍然显示 [200, 200] 而不是 [100, 200] 的列表
标签: python dictionary while-loop append