开头说明一下,用vscode和python自带的idle打开运行,geany会乱码里面的缩进也注意要修改
自定义一个函数
def login(users):
print('请输入您的用户信息')
_id = int(input('账号:'))
pwd = input('口令:')
for user in users:
u_id = user.get('id')
u_pwd = user.get('password')
if u_id == _id and pwd == u_pwd:
return user #结束for循环,将user作为函数的返回结果
print('--welcome use tao bank--')
opts = '1.opean(开户) 2.stockpile(存储) 3.withdrawal(取款) 0.exit(退出)'
数据存储
list[{'id':1,'name':'taoshukai','password':'123','money':0.0}]
datas = []
next_id = 1 #下一个开户的id编号
while True:
opt = input(opts)
# 验证输入的是否为数字
if not opt.isdigit():
print('you shuru feifa,please zhengque shuru')
continue
if opt == '1':
print('--请输入相关信息--')
name = input('姓名:')
pwd_cnt = 0
while pwd_cnt < 3:
password = input('口令:')
password2 = input('确认口令:')
if password == password2:
datas.append({'id':next_id,
'name': name,
'password':password,
'money':0.0})
print('-开户成功--')
print('账号:',datas[-1].get('id'))
next_id += 1
break
pwd_cnt += 1
if pwd_cnt == 3:
print('三次口令已经用完,请重新选择操作')
elif opt == '2':
'''
print('请输入您的账号和口令')
_id = int(input('账号:'))
password = input('口令:')
查询账号和口令是否存在并一致
_user = None
for user in datas:
user_id = user.get('id')
user_pwd = user.get('password')
if user.get('id') == _id and user_pwd == password:
_user = user
break
'''
_user = login(datas)
if not _user:
print('你输入的id账户不存在')
continue
print('登陆成功!')
money = input('存入的金额(元)😂
_user['money'] += float(money)
print('本次存款成功,余额:',_user['money'])
elif opt == '3':
_user = login(datas)
if not _user:
print('账号或口令不正确')
continue
print('登录成功')
money = float(input('本次取款金额(元)😂)
# 判断取款金额是否大于余额
if money <= _user.get('money'):
_user['money'] -= money
print('本次取款成功,余额:',_user.get('money'))
else:
print('余额不足,余额:',_user.get('money'))
else:
print('--退出成功--')
break # 退出循环