5.34 Write a function statement() that takes as input a list of floating-point numbers, with positive numbers representing deposits to and negative numbers representing withdrawals from a bank account. Your function should return a list of two floating-point numbers; the first will be the sum of the deposits, and the second (a negative number) will be the sum of the withdrawals. >>> statement([30.95, -15.67, 45.56, -55.00, 43.78]) [120.29, -70.67]
ANSWER:
def statement(numList):
deposits = 0
Withdrawals = 0
outcome= []
for number in numList:
if number<0:
withdrawals += numList
else
deposits += numList
outcome.append(withdrawals)
outcome.append(deposits)
return outcome
Explanation:
I have defined a function called as statement() that has a parameter numList. Inside the function, I have created two empty lists of floating point numbers, with positive numbers representing deposits and negative numbers representing withdrawals from a bank account.
Now I have created a for loop that iterates through the numList. Inside the loop, if a number is smaller than zero, add it to the withdrawals. If a number is greater than zero, add it to the deposits. When the loop is done, the fuction returns a list of two floating point numbers. The first will be the sum of the deposits, and the second (a negative number) will be the sum of the withdrawals.
Get Answers For Free
Most questions answered within 1 hours.