-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson-13.py
executable file
·54 lines (45 loc) · 1.18 KB
/
lesson-13.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
#!/usr/bin/env python3
# lesson-13.py - flatten the logic in main()
#
# Learn:
# code reorg: reduce nesting
def showBalance():
global balance
print('Your balance is $%d.' % balance)
print()
def withdrawal(amount):
global balance
if amount > balance:
print("Sorry, you don't have that much!")
else:
print('Withdraw $%d.' % amount)
balance -= amount
showBalance()
def deposit(amount):
global balance
print('Deposit $%d.' % amount)
balance += amount
showBalance()
def main():
print('Welcome to the bank.')
showBalance()
while True:
type = input('Enter d for deposit, w for withdrawal, or q to quit: ')
if type == 'q':
break
elif type != 'd' and type != 'w':
print('Invalid type. Please try again.')
continue
try:
amount = int(input('Enter amount: '))
except:
print('Invalid amount. Please try again.')
continue
if amount <= 0:
print('The amount must be positive.')
elif type == 'd':
deposit(amount)
else:
withdrawal(amount)
balance = 100
main()