그래서 문제는 입출력을 나타내는 양수와 함께 입출력을 나타내는 음수와 함께 부동 소수점의리스트를 입력으로받는 function 문()을 작성하는 것이 었습니다. 은행 계좌. 함수는 두 개의 부동 소수점 숫자 목록을 반환해야합니다. 첫 번째는 예금의 합계이고 두 번째 (음수)는 인출의 합계가됩니다.파이썬은 빈리스트를 정수로 변경합니다.
그리고 내가 작성한 것은 철회 빈 목록을 값 0으로 변경하는 것으로 보이므로 추가 기능을 사용할 수 없습니다. 파이썬이이 일을하고있는 이유가 있었는지 궁금 해서요, 아니면 단지 이상한 오류일까요?
def statement(lst):
"""returns a list of two numbers; the first is the sum of the
positive numbers (deposits) in list lst, and the second is
the sum of the negative numbers (withdrawals)"""
deposits, withdrawals, final = [], [], []
for l in lst:
print(l)
if l < 0:
print('the withdrawals are ', withdrawals) # test
withdrawals.append(l)
print('the withdrawals are ', withdrawals) # test
else:
print('the deposits are', deposits) # test
deposits.append(l)
print('the deposits are', deposits) # test
withdrawals = sum(withdrawals)
deposits = sum(deposits)
final.append(deposits)
final.append(withdrawals)
왜 다른 값으로 같은 이름을 사용하고 있습니까? 'withdrawals = sum (withdrawals) '은 더 이상'withdrawals'에 있었던 목록을 참조 할 방법이 없다는 것을 의미합니다. 그냥하지 마라. 문제가 없다.'total_withdrawals = sum (withdrawals)'. – abarnert