1. class bank_account: #创建一个class,叫bank_account
2. def __init__(self, name, account_number, balance): #初始化name,account_number,和balance
3. #self代表当前的实例
4. self.name = name
5. self.account_number = account_number
6. self.balance = balance
7. def make_deposit(self, deposit): #一个银行账户存钱的个方法,有一个参数,deposit,代表存钱数量
8. self.balance = self.balance + deposit #余额等于余额加上存钱的数量
9. print('Add $',deposit,'to the balance')
10.print('The balance now is', self.balance)
11.def make_withdrawal(self, withdrawal): #一个银行账户取钱的一个方法,有一个参数,withdrawal,代表取钱的数量
12. self.balance = self.balance - withdrawal #余额等于余额减去取钱的数量
13. if self.balance < 0: #如果钱不够了,就...
14. print('Not enough money')
15. self.balance = self.balance + withdrawal #把取过的钱再加到余额里,并且告诉银行账户的主人钱不够
16. else:
17. print('Subtract $', withdrawal, 'from the balance')
18. print('The balance now is', self.balance)
19. def display_balance(self): #一个银行账户看余额的一个方法
20. print('The balance is', self.balance)
21.
22.class saving_account(bank_account): #创建一个class,叫saving_account,是bank_account的子类
23. def __init__(self, name, account_number, balance, interest): #初始化name,account_number,balance,和interest
24. bank_account.__init__(self, name, account_number, balance) #调用父类的初始化函数,name,account_number,和balance
25. self.interest = interest
26.def get_balance(self, year): #一个存款账户看几年之后余额的一个方法
27. b = self.balance * ( 1 + self.interest )**year #复利公式,**是次方
28. print('The balance in', year, 'years, will be $', b)
29.
30.candy_account = saving_account('candy', '1112234', 10, 0.1) #创建一个saving_account,叫candy_account
31.
32.withdrawal_deposit_balance = str(input('Do you want to make withdrawal, make deposit, check balance, or check future balance?')) #问账户持有人要存钱,取钱,看现在的余额,还是看未来的余额
33.inputting = True
34.while inputting:
35. if withdrawal_deposit_balance == 'check balance': #如果账户持有者想要存钱,就...
36.candy_account.display_balance() #调用看现在的余额的方法
37.inputting = False
38.elif withdrawal_deposit_balance == 'withdrawal' or withdrawal_deposit_balance == 'make withdrawal': #如果账户持有者想要取钱,就...
39.dollars = float(input('How many dollars do you want to make withdrawal?')) #问账户持有者要取多少钱
40. candy_account.make_withdrawal(dollars) #调用取钱的方法
41. inputting = False
42.elif withdrawal_deposit_balance == 'deposit' or withdrawal_deposit_balance == 'make deposit': #如果账户持有者想要看现在的余额,就...
43. dollars = float(input('How many dollars do you want to make deposit')) #问账户持有者要存多少钱
44.candy_account.make_deposit(dollars) #调用存钱的方法
45.inputting = False
46. elif withdrawal_deposit_balance == 'future balance' or withdrawal_deposit_balance == 'check future balance': #如果账户持有者想要看未来的余额,就...
47. years = float(input('Which year do you want to check on? Please give the number of years after this year')) #问账户持有者又看几年之后的余额
48. candy_account.get_balance(years) #看未来的余额调用的方法
49. inputting = False
50. else:
51. print('Please enter make withdrawal, make deposit, check balance, or check future balance') #如果哪个都不是的话,就让账户持有者从新写一遍
Class练习题:银行账户操作
原创mb5ff2f24b42377 ©著作权
©著作权归作者所有:来自51CTO博客作者mb5ff2f24b42377的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章