我遇到了这个问题:
写一个函数语句(),它将浮点数列表作为输入,正数表示存款,负数表示从银行账户提款。你的函数应该返回一个两个浮点数的列表;第一个是存款的总和,第二个是提款的总和
代码我有: 导入数学
def statement (bank):
deposit = []
withdrawal = []
i = 0
for i in range(1, len(bank)):
if bank[i] >= 0:
deposit.append(bank[i])
elif bank[i] < 0:
withdrawal.append(bank[i])
print (sum(deposit, withdrawal))
statement([30.95, -15.67, 45.56, -55.00, 43.78])
问题在于我无法从列表中添加浮点数。我怎样才能做到这一点?谢谢
答案 0 :(得分:0)
这是我认为他们正在根据问题描述
寻找的def statement(bank):
deposit = 0
withdrawal = 0
for transaction in bank:
if transaction > 0:
deposit += transaction
else:
withdrawal += transaction
return [deposit, withdrawal]
列表应该有两个元素。第一个是所有存款的总和,第二个是所有提款的总和。
>>> statement([30.95, -15.67, 45.56, -55.00, 43.78])
[120.29, -70.67]