xonecas.io
387
Pe

python-exercices / files

python-exercices / level_two / ex_17.py

1.2 KB
1#!/usr/bin/env python3
2
3# Question 17
4# Level 2
5
6# Question: Write a program that computes the net amount of a bank account based a transaction
7# log from console input. The transaction log format is shown as following: D 100 W 200
8
9# D means deposit while W means withdrawal. Suppose the following input is supplied to the
10# program: D 300 D 300 W 200 D 100 Then, the output should be: 500
11
12# Hints: In case of input data being supplied to the question,
13# it should be assumed to be a console input.
14
15
16total_deposit = 0
17total_withdrawal = 0
18
19transactions = input('Enter the transaction log:\n').split()
20
21# We can assume the even indexes: 0, 2, 4, etc are the type of transaction.  And that the idx + 1
22# is the value of the transaction.
23pointer = 0
24while pointer < len(transactions):
25    transaction_type = transactions[pointer]
26    transaction_value = int(transactions[pointer + 1])
27
28    if transaction_type == 'W':
29        total_withdrawal = total_withdrawal + transaction_value
30    elif transaction_type == 'D':
31        total_deposit = total_deposit + transaction_value
32
33    pointer = pointer + 2  # 2 because we want to jump from even to even number.
34
35print(total_deposit - total_withdrawal)