-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson-23.py
executable file
·139 lines (120 loc) · 4.06 KB
/
lesson-23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# lesson-23.py - use float for amounts instead of int; update error checking and output format
#
# Learn:
# float literal
# string conversion to float
# format float field
# format field precision
# prevent accumulation error
# the regular expression module and re pattern matching
# raise an exception
# distinguish different types of exception (ValueError, Exception)
# how diligent you must be with floats
import re
import sys
class Account:
def __init__(self, balance):
self.balance = balance
self.startingBalance = balance
self.transactions = []
def showBalance(self):
print('Your balance is $%0.2f.' % self.balance)
print()
def showTransactions(self):
balance = self.startingBalance
print(' op amount balance')
print('-------- ---------- ----------')
print(' %10.2f (starting)' % balance)
for transaction in self.transactions:
[op, amount] = transaction
if op == 'w':
opLabel = 'withdraw'
balance -= amount
else:
opLabel = 'deposit'
balance += amount
print('%-8s %10.2f %10.2f' % (opLabel, amount, balance))
print()
def withdrawal(self, amount):
if amount > self.balance:
print("Sorry, you don't have that much!")
else:
print('Withdraw $%0.2f.' % amount)
self.balance = float('%.2f' % (self.balance - amount)) # prevent accumulation error
self.transactions.append(['w', amount])
self.showBalance()
def deposit(self, amount):
print('Deposit $%0.2f.' % amount)
self.balance = float('%.2f' % (self.balance + amount)) # prevent accumulation error
self.transactions.append(['d', amount])
self.showBalance()
def getOperation():
op = input('Enter d for deposit, w for withdrawal, t for transactions, or q to quit: ')
if op != 'q' and op != 'd' and op != 'w' and op != 't':
print('Invalid operation. Please try again.')
op = None
return op
def validateDollarAmount(amountStr):
tooMuchPrecision = re.compile('.*\.\d\d\d.*')
if tooMuchPrecision.match(amountStr):
raise Exception('You cannot supply fractions of a cent.')
def getAmount():
amount = None
try:
value = input('Enter amount: ')
amount = float(value)
if amount <= 0:
raise Exception('The amount must be positive.')
validateDollarAmount(value)
except ValueError:
print('Invalid amount. Please try again.')
except Exception as e:
print(e)
amount = None
return amount
def usage():
print('usage: %s [amount]' % sys.argv[0])
print('where amount is a starting balance dollar amount')
def getStartingBalance(balance):
if len(sys.argv) > 1:
try:
value = sys.argv[1]
balance = float(value)
if balance < 0:
raise Exception('The balance cannot be negative.')
validateDollarAmount(value)
except ValueError:
print('Invalid starting balance. Try something like 17.25 or 100 (the default).')
usage()
balance = None
except Exception as e:
print(e)
usage()
balance = None
return balance
def processTransactions(account):
while True:
amount = None
op = getOperation()
if op == 'q':
break
elif op == 't':
account.showTransactions()
elif op is not None:
amount = getAmount()
if amount is None:
pass
elif op == 'd':
account.deposit(amount)
else:
account.withdrawal(amount)
def main():
balance = 100.0
balance = getStartingBalance(balance)
if balance is not None:
print('Welcome to the bank.')
account = Account(balance)
account.showBalance()
processTransactions(account)
main()