python作业atm_python完成ATM(分目录)

importosimporthashlibimportjsonfrom conf importsettingsfrom lib importcommon#登录状态

status_dic ={'username': None,'status': False

}

flag=True

li=os.listdir(settings.user_path)#登录函数

deflogin():

count=0while count < 3:

username= input("请输入姓名:").strip()

password= input("请输入密码:").strip()

ret=hashlib.md5()

ret.update(password.encode("utf-8"))

pwd=ret.hexdigest()

with open(settings.user_path+'\\'+username +".json",encoding="utf-8") as f:

s=json.loads(f.readline())if username+'.json' in li and s[username] ==pwd:

status_dic["username"] =username

status_dic['status'] = True #状态改为True

print("登陆成功")returnTrueelse:print("账号或密码错误,请重新登录")

count+= 1

#注册函数

defregister():

count=0while count < 3:

username= input("请输入新账号:").strip()

password= input("请输新入密码:").strip()if username in li: #判断用户是否在文件中存在

print("用户名已存在,请重新输入")continue

elif not username.isalnum(): #判断用户是否是字母或数字组成

print("账号含有非法字符,请重新输入")continue

elif 5 < len(password) < 15: #限制密码在6-14位之间

dic ={}

md5=hashlib.md5()

md5.update(password.encode("utf-8"))

pwd=md5.hexdigest()

with open(settings.user_path+'\\'+username +".json", encoding="utf-8", mode="w") as f1:

dic[username]=pwd

dic.setdefault("money",0)

s=json.dumps(dic)

f1.write(s+"\n")print("恭喜!注册成功")returnTrueelse:print("密码不合规范,请重新输入")

count+= 1@common.authdeflook_money():print("您已进入查看余额功能")

with open(settings.user_path+ '\\' + status_dic["username"] + '.json', encoding="utf-8", mode="r") as f:

ret=json.loads(f.read())print(f'余额为:{ret["money"]}')

@common.authdefmoney():print("您已进入存钱功能")

c_money= input("请输入要存储的金额:").strip()

cq_money=int(c_money)

with open(settings.user_path+'\\'+status_dic["username"] +'.json',encoding="utf-8",mode="r") as f3,\

open(settings.user_path+'\\'+status_dic["username"] +'.bak',encoding="utf-8",mode="w") as f4:

ret=json.loads(f3.readline())

ret["money"] +=cq_money

s=json.dumps(ret)

f4.write(s)

os.remove(settings.user_path+'\\'+status_dic["username"] +'.json')

os.rename(settings.user_path+'\\'+status_dic["username"] +'.bak',settings.user_path+'\\'+status_dic["username"] +'.json')print(f"恭喜存钱成功")

@common.authdefzq_money():print("您已进入转账功能")

zz_user= input("请输入对方账户:").strip()

zz_money= int(input("请输入要转的金额:").strip())

zz_user1= zz_user + ".json"

if zz_user1 not in li: #判断用户是否在文件中存在

print("用户名不存在")else:

with open(settings.user_path+ '\\' + status_dic["username"] + '.json', encoding="utf-8", mode="r") as f1, \

open(settings.user_path+ '\\' + status_dic["username"] + '.bak', encoding="utf-8", mode="w") as f2:

ret=json.loads(f1.readline())

ret["money"] -=zz_money

s=json.dumps(ret)

f2.write(s)

os.remove(settings.user_path+ '\\' + status_dic["username"] + '.json')

os.rename(settings.user_path+ '\\' + status_dic["username"] + '.bak',

settings.user_path+ '\\' + status_dic["username"] + '.json')

with open(settings.user_path+ '\\' + zz_user1, encoding="utf-8", mode="r") as f2, \

open(settings.user_path+ '\\' + zz_user1+".bak", encoding="utf-8", mode="w") as f3:

ret=json.loads(f2.readline())

ret["money"] +=zz_money

s=json.dumps(ret)

f3.write(s)

os.remove(settings.user_path+ '\\' +zz_user1)

os.rename(settings.user_path+ '\\' + zz_user1+".bak",

settings.user_path+ '\\' +zz_user1)print("转账交易成功")

@common.authdefch_user():print("您已进入查看账户流水功能")

logger=common.record_logger()

logger.info(f'status_dic["username"]进行了流水操作')#退出程序

defexit_program():globalflag

flag=Falsereturnflagdefrun():

choice_dict={1: login,2: register,3: look_money,4: money,5: zq_money,6: ch_user,7: exit_program,

}whileflag:print('''欢迎来到ATM系统

1:请登录

2:请注册

3:查看余额

4:存钱

5:转账

6:查看账户流水

7:退出程序''')

choice= input('请输入您选择的序号:').strip()ifchoice.isdigit():

choice=int(choice)if 0 < choice <=len(choice_dict):

choice_dict[choice]()else:print("您输入的超出范围")else:print("输入不合规范,重新输入")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值