Python模拟自动存取款机的查询、存取款、修改密码等操作

时间:2022-09-15 10:14:33

1.工作流程

Python模拟自动存取款机的查询、存取款、修改密码等操作

2.模拟自动存取款机的操作

代码如下:

?
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
import msvcrt, sys, os
#定义用星号隐藏密码输入的函数
def psw_input():
  li = []
  while true:
    ch = msvcrt.getch()
    #回车
    if ch == b'\r':
      msvcrt.putch(b'\n')
      break
    #退格
    elif ch == b'\x08':
      if li:
        li.pop()
        msvcrt.putch(b'\b')
        msvcrt.putch(b' ')
        msvcrt.putch(b'\b')
    #esc
    elif ch == b'\x1b':
      break
    else:
      li.append(ch)
      msvcrt.putch(b'*')
  return li
#定义csdn银行atm欢迎界面的函数
def atm():
  '''
  csdn银行atm欢迎界面的函数
  '''
  print("="*14,"bank of csdn","="*14,"\n")
  print("{:^42}".format("atm"),"\n")
  print("="*14,"bank of csdn","="*14,"\n")
#csdn银行用户列表信息,用户信息包含:姓名、余额、密码(6位)、银行卡号(19位)
user_list = [{"name":"张 三","balance":10000,"password":"000000","numbers":"0000000000000000000"},
{"name":"李 四","balance":20000,"password":"111111","numbers":"1111111111111111111"},
{"name":"王 五","balance":30000,"password":"222222","numbers":"2222222222222222222"}]
#定义验证银行卡号与密码匹配的函数
def check(user_name,user_password):
  '''
  验证银行卡号与密码匹配的函数
  '''
  for i in range(len(user_list)):
    if user_name == user_list[i]["numbers"] and user_password == user_list[i]["password"]:
      return i #银行卡号与密码匹配则返回该用户在atm系统内的id值,否则返回none值
#定义用户登录成功后操作界面的函数
def interface():
  '''
  用户登录成功后操作界面的函数
  '''
  print("="*14,"用户操作界面","="*14,"\n")
  print("{0:2} {1:12} {2:12} {3:12}".format(" ","1. 查询","2. 取款","3. 存款"),"\n")
  print("{0:2} {1:10} {2:12}".format(" ","4. 修改密码","5. 退出"),"\n")
  print("="*42,"\n")
#定义用户查询信息的函数
def inquire(user_id):
  '''
  用户查询信息的函数
  '''
  print("="*14,"账号查询界面","="*14,"\n")
  print("|{0:<4}|{1:<18}|{2:<9}|\n".format("账户名","卡号","余额(rmb)"))
  print("|{0:<5}|{1:<20}|{2:<11}|\n".format(user_list[user_id]["name"],user_list[user_id]["numbers"],user_list[user_id]["balance"]))
#定义用户取款的函数
def withdrawal(amount):
  '''
  用户取款的函数
  '''
  i = user_list[user_id]["balance"]-int(amount)
  if i>=0:
    user_list[user_id]["balance"]-=int(amount)
  else:
    print("账户余额不足\n")
#定义用户存款的函数
def deposit(amount):
  '''
  用户存款的函数
  '''
  user_list[user_id]["balance"]+=int(amount)
#定义用户修改密码的函数
def change_password(old_password,new_password1,new_password2):
  '''
  用户修改密码的函数
  '''
  if old_password == user_list[user_id]["password"]:
    if new_password1 == new_password2:
      user_list[user_id]["password"] = new_password1
      print("新密码修改成功\n")
      return 1
    else:
      print("修改密码失败,您2次输入的新密码不一致\n")
      return 2
  else:
    print("旧密码输入错误\n")
    return 0
#用户登录界面,输入银行卡号和密码
chance = 3 #允许3次用户名或密码输入错误
while true:
  atm()
  user_name = input("请输入您的银行卡卡号:")
  print("")
  print("请输入您的银行卡密码:", end=' ', flush=true)
  user_password = b''.join(psw_input()).decode()
  print("")
  user_id = check(user_name,user_password)#验证银行卡号与密码是否匹配
  if user_id != none:
    print("登录成功\n")
    while true:
      interface()
      key_word = input("请输入操作选项:")
      print("")
      if key_word == "1":
        inquire(user_id)
        input("按任意键返回上一级目录:")
        print("")
      elif key_word == "2":
        print("="*14,"账号取款界面","="*14,"\n")
        amount = input("请输入取款金额:")
        print("")
        withdrawal(amount)
        inquire(user_id)
        input("按任意键返回上一级目录:")
        print("")
      elif key_word == "3":
        print("="*14,"账号存款界面","="*14,"\n")
        amount = input("请输入存款金额:")
        print("")
        deposit(amount)
        inquire(user_id)
        input("按任意键返回上一级目录:")
        print("")
      elif key_word == "4":
        print("="*14,"密码管理界面","="*14,"\n")
        print("请输入旧密码:", end=' ', flush=true)
        old_password = b''.join(psw_input()).decode()
        print("")
        print("请输入新密码:", end=' ', flush=true)
        new_password1 = b''.join(psw_input()).decode()
        print("")
        print("请再次输入新密码:", end=' ', flush=true)
        new_password2 = b''.join(psw_input()).decode()
        print("")       
        save = change_password(old_password,new_password1,new_password2)
        #如何检测到旧密码输入有误,将直接退出
        if save == 0:
          break
      elif key_word == "5":
        print("="*14,"欢迎下次光临","="*14,"\n")
        break
      else:
        print("="*14,"没有该选项","="*14,"\n")
  else:
    if chance > 1:    
      print("用户名或密码错误,您还有",chance-1,"次机会,请重新输入\n")
      chance -= 1
    else:
      print("对不起,您输入用户名或密码错误已达3次")
      break

3.运行结果

有以下初始用户信息备测试用:

姓名 银行卡号(19位) 密码(6位) 余额(rmb)
张 三 0000000000000000000 000000 10000
李 四 1111111111111111111 111111 20000
王 五 2222222222222222222 222222 30000

Python模拟自动存取款机的查询、存取款、修改密码等操作

输入卡号和密码进入用户操作界面

Python模拟自动存取款机的查询、存取款、修改密码等操作

查询余额界面

Python模拟自动存取款机的查询、存取款、修改密码等操作

取款界面

Python模拟自动存取款机的查询、存取款、修改密码等操作

存款界面

Python模拟自动存取款机的查询、存取款、修改密码等操作

修改密码界面

总结

以上所述是小编给大家介绍的python模拟自动存取款机的查询、存取款、修改密码等操作,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

原文链接:https://blog.csdn.net/momobaba2018/article/details/82263744