# 编写ATM程序实现下述功能
# 1.充值功能:用户输入充值钱数,该账号钱数完成修改
# 2.转账功能:用户A向用户B转账1000元,完成用户A账号减钱,用户B账号加钱
# 3.提现功能:用户输入提现金额,该账号钱数减少
# 4.查询余额功能:输入账号查询余额
以上功能需要在用户登录后才可操作,否则用户需要重新注册
import os
#定义一个全局变量:字典、登陆后的用户名
dict={}
login_user=None
def pay_money(username):
"""用户输入充值金额,保存到全局变量字典中"""
while True:
money=(input("请输入您要充值的金额:").strip())
if not money.isdigit():
print('输入的必须是数字且为整数!')
continue
else:
pay_money=int(money)
dict[username]+=pay_money
print("您已充值成功!")
break
save_data()
def transfer(username):
""""转账,把A用户的钱加到B账户中,A减少金额,B增加金额"""
transfer_name = input("输入您要转账的账户名:").strip()
if transfer_name in dict:
while True:
money = input("请输入您要转账的金额:").strip()
if money.isdigit():
pay_money = int(money)
if pay_money <= dict[username]:
dict[username] -= pay_money
dict[transfer_name] += pay_money
print("您已转账成功!")
break
elif pay_money > dict[username]:
print('您输入的转账金额超过了实际金额,请重新输入提现金额:')
continue
else:
print('输入的必须是数字且为整数!')
continue
elif transfer_name not in dict:
print("该用户名不存在,请重新核实!")
save_data()
def withdraw(username):
"""提现,用户输入金额,该账号金额减少"""
while True:
money = input("请输入您要提现的金额:").strip()
if money.isdigit():
pay_money = int(money)
if pay_money<=dict[username]:
dict[username] -= pay_money
print("您已提现成功!")
break
elif pay_money > dict[username]:
print('您输入的提现金额超过了实际金额,请重新输入提现金额:')
continue
else:
print('输入的必须是数字且为整数!')
continue
save_data()
def check_money(username):
"""查询余额,输入该账户,输出该账号的金额数"""
print("您的账户余额为:%s元" %dict[username])
def login():
"""用户登录,校验用户是否存在,用户存在即才可以做其他操作,输错密码三次该账号被锁定进黑名单"""
count=0
while count<3:
user_name = input("请输入您的账户名:").strip()
if user_name not in dict:
print("该用户名不存在,请先注册!")
return False
password = input("请输入您的账户密码:").strip()
with open("black_name.txt", mode="r", encoding="utf-8") as f:
for line1 in f:
blackname = line1.strip()
with open("user_name.txt","rt",encoding='utf-8') as file:
for line in file:
username,use_psd=line.strip().split(":")
if user_name == username and blackname == username:
print("该账号已被锁定!!")
count=3
break
if user_name==username and password==use_psd and user_name in dict :
print("登陆成功!")
global login_user
login_user=user_name
count=3
return True
else:
count += 1
print('您已输错%s次,还剩%s次' % (count, 3 - count))
if count == 3:
with open("black_name.txt", mode='wt', encoding='utf-8') as f:
f.write(f'{user_name}
')
print('该账号已被锁定加入黑名单!')
break
def registered():
# 定义一个空字典,把用户信息里面的信息都存入到reg_dict字典里
reg_dict={}
with open("user_name.txt", "rt", encoding='utf-8') as file:
for line in file:
user_name, user_psd = line.strip().split(":")
reg_dict[user_name] = user_psd
flag=True
while flag:
username = input("请您输入用户名:").strip()
password = input("请您输入密码:").strip()
password1= input("请您再次输入密码:").strip()
if username not in reg_dict and password == password1:
with open("user_name.txt", mode='at', encoding='utf-8') as f: # 追加写入到用户信息文件中
f.write(f'{username}:{password}
')
with open("user_money.txt", "at", encoding='utf-8') as f1: # 追加写入到用户金额文件中
f1.write(f'{username}:{0}
')
print("账号注册成功!")
flag = False
break
elif password != password1:
print("两次密码输入不一样,请重新注册!")
elif username in reg_dict:
print("该用户名已经存在,请重新注册!")
def save_data():
"""保存数据,将字典里的数据写入文件"""
with open("user_money.txt", "wt", encoding="utf-8") as file:
for line in dict:
file.write(f'{line}:{dict[line]}
')
def show_menu():
"""展示功能菜单栏"""
print("==========ATM系统v1.0========")
print("1. 充值")
print("2. 转账")
print("3. 提现")
print("4. 查询")
print("5. 退出")
def start():
"""首先读取useer_money文件数据放到全局变量字典里面"""
global dict
with open("user_money.txt", "rt", encoding='utf-8') as file:
for line in file:
user_name, use_money = line.strip().split(":")
dict[user_name] = int(use_money)
flag = login()
if flag==0:
registered()
while flag:
show_menu()
menu_option = input("请输入操作的功能选项:")
if menu_option == "1":
pay_money(login_user)
elif menu_option == "2":
transfer(login_user)
elif menu_option == "3":
withdraw(login_user)
elif menu_option == "4":
check_money(login_user)
elif menu_option == "5":
#把数据保存到文件中
save_data()
print("退出系统")
break
start()