Python编写一个简易银行账户系统,供大家参考,具体内容如下
文章中主要涉及的方法是Python中的open(filename, ‘r')以读的方式打开文件open(filename, ‘w')以写的方式打开文件我们用for * in *读取文件中的数据或者写入文件数据 用dict(eval(list2))方法来把字符串转化为字典。
详细代码如下
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
import math import re def main(): # 主函数 select = True while (select): menu() start_int = input ( "请选择你您想要操作功能的序号:" ) if start_int = = "12" : select = False print ( "你已经退出系统欢迎下次在来" ) elif start_int = = "4" : insert() elif start_int = = "5" : login() elif start_int = = "6" : show() elif start_int = = "11" : delete() elif start_int = = "7" : revise() elif start_int = = "8" : deposit() elif start_int = = "9" : getMoney() elif start_int = = "10" : UseMoney() def menu(): # 菜单显示 print ( "1========银行存取钱系统========" ) print ( "2===========================" ) print ( "3===========功能菜单===========" ) print ( "4=========注册个人信息==========" ) print ( "5============登入=============" ) print ( "6=========查询个人信息==========" ) print ( "7=========修改个人账户==========" ) print ( "8============存钱=============" ) print ( "9============取钱=============" ) print ( "10=========显示年收益==========" ) print ( "11========注销个人信息==========" ) print ( "12===========退出=============" ) filename = "Bank.txt" # 定义保存用户信息的文件名 def save(Bank): # 创建文件方法 try : Bank_txt = open (filename, "a" ) except Exception as e: Bank_txt = open (filename, "w" ) for info in Bank: Bank_txt.write( str (info) + "\n" ) Bank_txt.close() def insert(): # 注册方法 BankList = [] # 保存用户信息列表 mark = True # 是否继续添加 while mark: id = input ( "请输入您的ID密码(如1001):" ) if not id : break name = input ( "请输入姓名" ) if not name: break try : deposit = int ( input ( "输入你要存款的金额" )) if deposit = = 0 : break except : print ( "输入无效,不是输入整型数,请重新输入" ) continue Bank = { "id" : id , "name" : name, "deposit" : deposit} BankList.append(Bank) mark = False save(BankList) print ( "注册成功" ) global g_select g_select = 0 global Username global Userpassword def login(): # 登入方法 global Username global g_select global g_BankQuery global Userpassword g_BankQuery = [] Username = str ( input ( "请输入您的用户名" )) Userpassword = str ( input ( "请输入您的密码" )) file = open (filename, 'r' ) Bank = file .readlines() # 读取全部内容 for list in Bank: d = dict ( eval ( list )) # 字符转化为字典 if d[ 'name' ] = = Username and d[ 'id' ] = = Userpassword: g_BankQuery.append(d) print ( "登入成功!" ) g_select = 1 else : pass if not g_BankQuery: g_select = 0 print ( "登入失败请先注册!" ) else : pass def show(): # 查询个人信息 if g_select = = 1 : format_title = "{:^6}{:^12}" print (format_title. format ( "名字" , "存款" )) format_date = "{:^6}{:^12}" for info in g_BankQuery: print (format_date. format ( str (info.get( 'name' )), str (info.get( 'deposit' )))) else : print ( "请先登入!" ) def delete(): # 删除个人账户方法 global g_BankQuery cz = [] global g_select choose = 0 if g_select = = 1 : while choose < 3 : username = str ( input ( "请输入你姓名" )) userpassword = str ( input ( "请输入您的密码" )) file = open (filename, 'r' ) Bank = file .readlines() # 读取全部内容 for list in Bank: d = dict ( eval ( list )) # 字符转化为字典 if d[ 'name' ] = = username and d[ 'id' ] = = userpassword: cz.append(d) file .close() choose = 3 NewBank = open (filename, 'w' ) # 以写的方式打开文件 for list2 in Bank: d2 = dict ( eval (list2)) # 字符转化为字典 if d2[ 'name' ] ! = username and d2[ 'id' ] ! = userpassword: NewBank.write( str (d2) + "\n" ) else : pass else : pass if not cz: choose = choose + 1 if choose = = 3 : g_select = 0 print ( "请重新登入!" ) else : print ( "用户名或者密码错误,请重新输入你还有:" + str ( 3 - choose) + "机会" ) else : g_BankQuery.clear() g_select = 0 print ( "您的个人信息已经注销" ) else : print ( "请先登入!" ) def revise(): # 修改个人账户方法 cz = [] global g_select if g_select = = 1 : username = input ( "请输入您的用户名:" ) userpassword = input ( "请输入您的密码:" ) file = open (filename, 'r' ) Bank = file .readlines() # 读取全部内容 for list in Bank: d = dict ( eval ( list )) # 字符转化为字典 if d[ 'name' ] = = username and d[ 'id' ] = = userpassword: cz.append(d) file .close() NewBank = open (filename, 'w' ) # 以写的方式打开文件 for list2 in Bank: d2 = dict ( eval (list2)) # 字符转化为字典 if d2[ 'name' ] = = username and d2[ 'id' ] = = userpassword: d2[ 'name' ] = input ( "输入您的新名字:" ) d2[ 'id' ] = input ( "输入您的新密码:" ) NewBank.write( str (d2) + "\n" ) print ( "修改成功,请重新登入!" ) g_select = 0 else : NewBank.write( str (d2) + "\n" ) else : pass if not cz: print ( "你输入的密码或者用户名有误请重新登入" ) g_select = 0 else : pass else : print ( "请先登入!" ) def deposit(): # 存钱方法 global g_BankQuery global g_select cz = [] if g_select = = 1 : money = int ( input ( "请输入你要存多少钱:" )) file = open (filename, 'r' ) Bank = file .readlines() # 读取全部内容 for list in Bank: d = dict ( eval ( list )) # 字符转化为字典 if d[ 'name' ] = = Username and d[ 'id' ] = = Userpassword: cz.append(d) file .close() NewBank = open (filename, 'w' ) # 以写的方式打开文件 for list2 in Bank: d2 = dict ( eval (list2)) # 字符转化为字典 if d2[ 'name' ] = = Username and d2[ 'id' ] = = Userpassword: d2[ 'deposit' ] = str ( int (d2[ 'deposit' ]) + money) NewBank.write( str (d2) + "\n" ) print ( "储存成功!" ) g_BankQuery.clear() g_BankQuery.append(d2) else : NewBank.write( str (d2) + "\n" ) else : pass else : print ( "请先登入!" ) def getMoney(): # 取钱方法 global g_select global g_BankQuery cz = [] if g_select = = 1 : money = int ( input ( "请输入你要取多少钱:" )) file = open (filename, 'r' ) Bank = file .readlines() # 读取全部内容 for list in Bank: d = dict ( eval ( list )) # 字符转化为字典 if d[ 'name' ] = = Username and d[ 'id' ] = = Userpassword: cz.append(d) if money > int (d[ 'deposit' ]): print ( "您的余额不足" ) else : file .close() NewBank = open (filename, 'w' ) # 以写的方式打开文件 for list2 in Bank: d2 = dict ( eval (list2)) # 字符转化为字典 if d2[ 'name' ] = = Username and d2[ 'id' ] = = Userpassword: d2[ 'deposit' ] = str ( int (d2[ 'deposit' ]) - money) NewBank.write( str (d2) + "\n" ) print ( "取钱成功!" ) g_BankQuery.clear() g_BankQuery.append(d2) else : NewBank.write( str (d2) + "\n" ) else : pass else : print ( "请先登入!" ) def UseMoney(): # 利息计算 UM = True while UM: try : money = float ( input ( "请输入你要投资理财多少钱:" )) year = int ( input ( "请你输入你要储存多少年:" )) except : print ( "请你输入整数年份!" ) if 0 < year < = 3 : profitmargin = 0.03 elif 3 < year < = 5 : profitmargin = 0.04 elif 5 < year < = 10 : profitmargin = 0.06 elif year > 10 : profitmargin = 0.08 if money < 0 or year < = 0 : print ( "您的本金不能少于0元或者年份不能少于0年" ) else : UM = False profit = round (money * year * profitmargin, 3 ) print ( "你储存:" + str (year) + "年将获得的利润会等于:" + str (profit) + "元本金加利润会等于:" + str (profit + money) + "元" ) if __name__ = = "__main__" : |
运行图片:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/nanxiang11/article/details/112974649