基于Python实现ATM操作系统
概要
基于Python语言,实现ATM的基本功能,包括查询余额、存款、取款、转账、注册等基本功能。能够实现用户和管理员两种身份识别,管理员有权限对系统做出修改,包括提升用户级别、关闭系统等操作。用户信息保存于user_data.txt文件中,也可自行接入数据库。
整体架构流程
# 选择及初始
def main() :
read_txt()
global c_level
global list2
print("--------------------mini_ATM--------------------")
print("")
print("用户\t\t【请输入1】")
print("管理员\t\t【请输入2】")
print("")
a = input("请输入您的选择:")
if int(a) == 1 :
clear()
print("--------------------mini_ATM--------------------")
print("")
jdt("正在加载,请稍等",0.01)
clear()
c_start_first()
elif int(a) == 2 :
clear()
print("--------------------mini_ATM--------------------")
print("")
jdt("正在加载,请稍等",0.01)
clear()
g_start()
else:
clear()
print("--------------------mini_ATM--------------------")
print("")
print("您的输入有误,请在3秒后重新输入!")
time.sleep(3)
clear()
main()
# 用户系统主页
def c_start_first() :
print("--------------------用户系统--------------------")
print("")
print("登录\t\t【请输入1】")
print("注册\t\t【请输入2】")
print("")
cz = input("请输入您的选择:")
if int(cz) == 1:
clear()
print("--------------------用户系统--------------------")
print("")
jdt("正在加载,请稍等",0.01)
clear()
start()
elif int(cz) == 2:
clear()
print("--------------------用户系统--------------------")
print("")
jdt("正在加载,请稍等",0.01)
clear()
enroll()
else:
clear()
print("--------------------用户系统--------------------")
print("")
print("您的输入有误,请在3秒后重新输入!")
time.sleep(3)
clear()
c_start_first()
# 注册
def enroll() :
global list2
global c_id
global c_id_1
global c_name
global c_sex
global c_sfz
global c_money
global c_limit
global c_password_2
global c_year
global c_date
global c_phone
global c_level
random_id()
if c_id not in list1:
random_money()
random_limit()
print("--------------------用户注册--------------------")
print("")
c_name = input("请输入姓名:")
print("")
c_phone = int(input("请输入手机号:"))
print("")
if len(str(c_phone)) == 11 :
c_sfz = input("请输入身份证:")
print("")
if len(c_sfz) == 18:
c_level = int(1)
c_year = str(c_sfz)[6:10]
c_date = str(c_sfz)[10:14]
if int(c_sfz[16]) % 2 == 1:
c_sex = "男"
elif int(c_sfz[16]) % 2 == 0:
c_sex = "女"
if c_sfz not in list2 :
clear()
create_password()
with open("user_data.txt", "a", encoding="utf-8") as f:
f.write(f"\n{c_name},{c_id},{c_password},{c_sex},{c_sfz},{c_money},{c_limit},{c_year},{c_date},{c_phone},{c_level}")
f.flush()
f.close()
clear()
print("--------------------用户注册--------------------")
print("")
jdt("注册中,请稍等",0.03)
clear()
print("--------------------用户注册--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,恭喜您注册成功!")
print(f"您的银行卡号为:{c_id},密码为:{c_password_2},日限额为:{c_limit}元。")
print("")
else:
print(f"{c_name} 女士,您好,恭喜您注册成功!")
print(f"您的银行卡号为:{c_id},密码为:{c_password_2},日限额为:{c_limit}元。")
print("")
else:
clear()
print("--------------------用户注册--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,身份证号:{c_sfz}已被注册!请您直接登录!")
time.sleep(1)
print("")
print("系统将在5秒后自动返回主页!")
time.sleep(5)
clear()
main()
else:
print(f"{c_name} 女士,您好,身份证号:{c_sfz}已被注册!请您直接登录!")
time.sleep(1)
print("")
print("系统将在5秒后自动返回主页!")
time.sleep(5)
clear()
main()
else:
print("您的身份证信息输入有误!请检查后重新输入。")
time.sleep(3)
clear()
enroll()
else:
print("您输入的手机号有误,请检查后重新输入。")
time.sleep(3)
clear()
enroll()
else:
enroll()
try:
cs = func_timeout(20, lambda: int(input("系统将在20秒后自动跳转至主页面!如需直接登录,请输入0:")))
if cs == 0:
clear()
return start()
except FunctionTimedOut:
enter()
clear()
print("感谢您的使用!")
print("")
print("再见!")
time.sleep(5)
clear()
return main()
# 用户登录
def start() :
read_txt()
global c_id # 用户id
global c_password
global c_name
global c_money
global c_limit
global c_sex
global c_sfz
global c_year
global c_date
global c_phone
global c_level
print("--------------------用户登录--------------------")
print("")
c_id = int(input("请输入您的账号:"))
print("")
if c_id in list1:
password()
else:
clear()
print("--------------------用户登录--------------------")
print("")
print("您的账号信息输入有误!请检查后重新输入。")
time.sleep(5)
clear()
start()
# 用户登陆后主菜单
def main_view() :
global c_name
global c_sex
global c_year
global c_date
global c_level
while True:
clear()
if c_date == now_date :
print("--------------------用户菜单--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,今天是您的{int(now_year) - int(c_year)}岁生日,祝您生日快乐!")
print("")
else:
print(f"{c_name} 女士,您好,今天是您的{int(now_year) - int(c_year)}岁生日,祝您生日快乐!")
print("")
print("查询余额\t【请输入1】")
print("存款\t\t【请输入2】")
print("取款\t\t【请输入3】")
print("转账\t\t【请输入4】")
print("修改密码\t【请输入5】")
print("退出登录\t【请输入6】")
print("")
else:
print("--------------------用户菜单--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好!")
print("")
else:
print(f"{c_name} 女士,您好!")
print("")
print("查询余额\t【请输入1】")
print("存款\t\t【请输入2】")
print("取款\t\t【请输入3】")
print("转账\t\t【请输入4】")
print("修改密码\t【请输入5】")
print("退出登录\t【请输入6】")
print("")
cz = input("请输入您的选择:")
if int(cz) == 1 :
clear()
print("--------------------查询余额--------------------")
print("")
jdt("查询中,请稍等",0.01)
last_money()
elif int(cz) == 2 :
clear()
print("--------------------用户菜单--------------------")
print("")
jdt("正在加载,请稍等",0.01)
in_money()
elif int(cz) == 3 :
clear()
print("--------------------用户菜单--------------------")
print("")
jdt("正在加载,请稍等",0.01)
out_money()
elif int(cz) == 4 :
clear()
print("--------------------用户菜单--------------------")
print("")
jdt("正在加载,请稍等",0.01)
get_money()
elif int(cz) == 5 :
clear()
print("--------------------用户菜单--------------------")
print("")
jdt("正在加载,请稍等",0.01)
change_password()
elif int(cz) == 6:
clear()
print("--------------------退出登录--------------------")
print("")
jdt("退出中,请稍等", 0.01)
clear()
print("--------------------退出登录--------------------")
print("")
print("您已成功退出登录!")
print("")
print("欢迎您下次光临!")
print("")
print("再见!")
time.sleep(5)
clear()
main()
else:
clear()
print("--------------------用户菜单--------------------")
print("")
print("您的输入有误,请在3秒后重新输入!")
time.sleep(3)
continue
# 查询余额
def last_money() :
clear()
print("--------------------查询余额--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,您的账户余额为:{c_money}元。")
print("")
else:
print(f"{c_name} 女士,您好,您的账户余额为:{c_money}元。")
print("")
a = int(input("返回用户菜单 【请输入0】"))
if a == 0:
clear()
print("--------------------返回用户菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
return main_view()
# 存款
def in_money() :
clear()
print("--------------------存款--------------------")
print("")
global c_money
global cun
global c_id
global list1
cun = int(input("请输入您的存款金额:"))
print("")
if c_limit >= cun:
c_money += cun
for i in all_list:
if int(i[1]) == c_id:
i[5] = c_money
clear()
print("--------------------存款--------------------")
print("")
jdt("存款中,请稍等",0.02)
clear()
print("--------------------存款--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,您已成功存款{cun}元,您的账户余额为:{c_money}元。")
print("")
else :
print(f"{c_name} 女士,您好,您已成功存款{cun}元,您的账户余额为:{c_money}元。")
print("")
update_txt()
else :
if c_sex == "男":
print(f"{c_name} 先生,您好,本次存款失败,您的账户今日限额为:{c_limit}元。")
print("")
else :
print(f"{c_name} 女士,您好,本次存款失败,您的账户今日限额为:{c_limit}元。")
print("")
a = int(input("返回用户菜单 【请输入0】"))
if a == 0:
clear()
print("--------------------返回用户菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
return main_view()
# 取款
def out_money() :
clear()
print("--------------------取款--------------------")
print("")
global c_money
global qu
global list1
qu = int(input("请输入您的取款金额:"))
print("")
if c_money >= qu :
if c_limit >= qu :
c_money -= qu
for i in all_list:
if int(i[1]) == c_id:
i[5] = c_money
clear()
print("--------------------取款--------------------")
print("")
jdt("取款中,请稍等",0.02)
clear()
print("--------------------取款--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,您已成功取款{qu}元,您的账户余额为:{c_money}元。")
print("")
else :
print(f"{c_name} 女士,您好,您已成功取款{qu}元,您的账户余额为:{c_money}元。")
print("")
update_txt()
else :
if c_sex == "男":
print(f"{c_name} 先生,您好,本次取款失败,您的账户今日限额为:{c_limit}元。")
print("")
else :
print(f"{c_name} 女士,您好,本次取款失败,您的账户今日限额为:{c_limit}元。")
print("")
else :
if c_sex == "男":
print(f"{c_name} 先生,您好,本次取款失败,余额不足!您的账户余额为:{c_money}元。")
print("")
else :
print(f"{c_name} 女士,您好,本次取款失败,余额不足!您的账户余额为:{c_money}元。")
print("")
a = int(input("返回用户菜单 【请输入0】"))
if a == 0:
clear()
print("--------------------返回用户菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
return main_view()
# 转账
def get_money() :
clear()
print("--------------------转账--------------------")
print("")
global c_id, shou_money, shou_name, shou_passsword, shou_sex, shou_sfz, shou_limit
global c_money
global shou_year
global shou_date
global shou_phone
global shou_level
global c_year
global c_date
global c_phone
global c_level
global list1
shou_id = int(input("请输入对方账户:"))
print("")
if shou_id in list1 :
if shou_id != c_id :
for i in all_list:
if int(i[1]) == shou_id:
shou_money = int(i[5])
shou_name = i[0]
shou_id = int(i[1])
shou_passsword = i[2]
shou_sex = i[3]
shou_sfz = i[4]
shou_limit = i[6]
shou_year = i[7]
shou_date = i[8]
shou_phone = int(i[9])
shou_level = int(i[10])
# print(shou_level)
if shou_level != 2 :
zhuan_money = int(input("请输入转账金额:"))
print("")
if c_money >= zhuan_money :
if c_limit >= zhuan_money :
c_money -= zhuan_money
shou_money += zhuan_money
with open("user_data.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
line = line.strip() # 去掉换行符
words = line.split(",") # 按照","进行分割
# print(words)
list3.append(int(words[1]))
list1 = list({}.fromkeys(list3).keys())
list2.append(words[4])
all_list.append(words)
# print(list1)
if list1.index(shou_id) == len(list1) - 1 :
# print("yes")
file[list1.index(shou_id)] = f"{shou_name},{shou_id},{shou_passsword},{shou_sex},{shou_sfz},{shou_money},{shou_limit},{shou_year},{shou_date},{shou_phone},{shou_level}"
else :
# print("no")
file[list1.index(shou_id)] = f"{shou_name},{shou_id},{shou_passsword},{shou_sex},{shou_sfz},{shou_money},{shou_limit},{shou_year},{shou_date},{shou_phone},{shou_level}\n"
with open("user_data.txt", "w+", encoding="utf-8") as f:
f.writelines(file)
f.flush()
for i in all_list:
if int(i[1]) == c_id:
i[5] = c_money
clear()
print("--------------------转账--------------------")
print("")
jdt("转账中,请稍等",0.02)
clear()
print("--------------------转账--------------------")
print("")
if c_sex == "男":
if shou_sex == "男" :
print(f"{c_name} 先生,您好,您向 {shou_name} 先生成功转账{zhuan_money}元,您的账户余额为:{c_money}元。")
print("")
else :
print(f"{c_name} 先生,您好,您向 {shou_name} 女士成功转账{zhuan_money}元,您的账户余额为:{c_money}元。")
print("")
else :
if shou_sex == "男":
print(f"{c_name} 女士,您好,您向 {shou_name} 先生成功转账{zhuan_money}元,您的账户余额为:{c_money}元。")
print("")
else :
print(f"{c_name} 女士,您好,您向 {shou_name} 女士成功转账{zhuan_money}元,您的账户余额为:{c_money}元。")
print("")
update_txt()
else :
if c_sex == "男":
print(f"{c_name} 先生,您好,本次转账失败,您的账户今日限额为:{c_limit}元。")
print("")
else :
print(f"{c_name} 女士,您好,本次转账失败,您的账户今日限额为:{c_limit}元。")
print("")
else :
if c_sex == "男":
print(f"{c_name} 先生,您好,本次转账失败,余额不足!您的账户余额为:{c_money}元。")
print("")
else :
print(f"{c_name} 先生,您好,本次转账失败,余额不足!您的账户余额为:{c_money}元。")
print("")
else:
print("收款方账号已注销,请核对后再次进行操作。")
time.sleep(3)
clear()
main_view()
else :
print("收款方账号不能和转出方账号一致!")
print("请在3秒后重新输入!")
time.sleep(3)
get_money()
else :
print("收款方信息有误,请核实后重新输入!")
time.sleep(3)
get_money()
a = int(input("返回用户菜单 【请输入0】"))
if a == 0:
clear()
print("--------------------返回用户菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
return main_view()
# 修改密码
def change_password() :
global c_password
global list1
clear()
print("--------------------修改密码--------------------")
print("")
old_c_password = int(input("请输入您的旧密码:"))
print("")
for i in all_list:
if int(i[1]) == c_id:
c_password_1 = i[2]
# print(c_password_1)
if old_c_password == int(c_password_1) :
new_c_password_1 = int(input("请输入您的新密码:"))
if len(str(new_c_password_1)) == 6 :
print("")
new_c_password_2 = int(input("请再次输入您的新密码:"))
print("")
if new_c_password_2 == new_c_password_1 :
i[2] = new_c_password_2
c_password = new_c_password_2
clear()
print("--------------------修改密码--------------------")
print("")
jdt("修改中,请稍等",0.02)
clear()
print("--------------------修改密码--------------------")
print("")
if c_sex == "男":
print(f"{c_name} 先生,您好,您的密码修改成功!请重新登录。")
print("")
else :
print(f"{c_name} 女士,您好,您的密码修改成功!请重新登录。")
print("")
update_txt()
time.sleep(5)
clear()
main()
else :
print("两次新密码不一致,请检查后重新输入!")
time.sleep(3)
change_password()
else:
print("密码位数有误!请检查后重新输入6位数密码。")
time.sleep(3)
change_password()
else :
print("密码输入错误,请重新输入!")
time.sleep(3)
change_password()
# 管理员登陆
def g_start() :
read_txt()
global c_id # 用户id
global c_password
global c_name
global c_money
global c_limit
global c_sex
global c_sfz
global c_year
global c_date
global c_phone
global c_level
print("--------------------管理员登录--------------------")
print("")
c_id = int(input("请输入您的账号:"))
print("")
if c_id in list1:
g_password()
else:
clear()
print("--------------------管理员登录--------------------")
print("")
print("您的账号信息输入有误!请检查后重新输入。")
time.sleep(5)
clear()
g_start()
# 管理员登陆后主菜单
def g_main_view():
clear()
global c_name
global c_sex
global c_year
global c_date
global c_phone
global c_level
while True:
print("--------------------管理员菜单--------------------")
print("")
print(f"{c_name} 管理员,您好!欢迎使用管理员系统。")
print("")
print("找回密码\t【请输入1】")
print("权限管理\t【请输入2】")
print("删除账户\t【请输入3】")
print("关闭程序\t【请输入4】")
print("返回首页\t【请输入0】")
print("")
cz = input("请输入您的选择:")
if int(cz) == 1 :
clear()
print("--------------------管理员菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
find_password()
elif int(cz) == 2 :
clear()
print("--------------------管理员菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
qxgl()
elif int(cz) == 3 :
clear()
print("--------------------管理员菜单--------------------")
print("")
jdt("正在加载,请稍等", 0.01)
clear()
del_data()
elif int(cz) == 4 :
clear()
print("--------------------关闭程序--------------------")
print("")
print("感谢您的使用!")
print("")
print("再见!")
time.sleep(3)
clear()
print("--------------------关闭程序--------------------")
print("")
jdt("程序正在关闭,请稍等",0.05)
os._exit(0)
elif int(cz) == 0 :
clear()
print("--------------------返回首页--------------------")
print("")
print("感谢您使用管理员系统!")
print("")
print("再见!")
time.sleep(3)
clear()
main()
else:
clear()
print("--------------------管理员菜单--------------------")
print("")
print("您的输入有误,请在3秒后重新输入!")
time.sleep(3)
continue
# 找回密码
def find_password() :
global f_level
print("--------------------找回密码--------------------")
print("")
f_id = int(input("请输入待服务账号:"))
print("")
if f_id in list1 :
for i in all_list :
if f_id == int(i[1]) :
f_level = int(i[10])
if int(f_level) != int(2) :
f_sfz_1 = i[4]
f_sfz = input("请输入客户身份证号:")
print("")
if f_sfz == f_sfz_1 :
clear()
f_password = i[2]
clear()
print("--------------------找回密码--------------------")
print("")
jdt("查询中,请稍等",0.02)
clear()
print("--------------------找回密码--------------------")
print("")
print(f"账号{f_id}的密码为:{f_password}。")
print("")
print("请提醒用户及时修改密码!")
print("")
print("15秒后将自动返回管理员系统主页。")
time.sleep(15)
g_main_view()
else:
print("客户身份证号有误,请查证后重新输入!")
time.sleep(3)
clear()
find_password()
else:
clear()
print("--------------------找回密码--------------------")
print("")
print("该账户已删除,请重新核实。")
time.sleep(3)
g_main_view()
else:
print("账号输入有误,请查证后重新输入!")
time.sleep(3)
clear()
find_password()
# 权限管理
def qxgl() :
global list1
global f_money
global f_limit
global f_password
global f_id
global f_name
global f_sex
global f_sfz
global f_year
global f_date
global f_phone
global f_level
print("--------------------权限管理--------------------")
print("")
print("增添权限\t【请输入1】")
print("移除权限\t【请输入2】")
print("")
cz = input("请输入您的选择:")
clear()
if int(cz) == 1 :
print("--------------------增添权限--------------------")
print("")
f_id = int(input("请输入待服务账号:"))
print("")
if f_id in list1 :
for i in all_list :
if f_id == int(i[1]) :
f_name = i[0]
f_password = int(i[2])
f_sex = i[3]
f_sfz = i[4]
f_money = int(i[5])
f_limit = int(i[6])
f_year = int(i[7])
f_date = i[8]
f_phone = int(i[9])
f_level = int(i[10])
if f_level != 2 :
if f_level == 0 :
clear()
print("--------------------增添权限--------------------")
print("")
if f_sex == "男":
print(f"账号{f_id},{f_name} 先生拥有管理员权限。")
else:
print(f"账号{f_id},{f_name} 女士拥有管理员权限。")
time.sleep(3)
g_main_view()
else:
clear()
print("--------------------增添权限--------------------")
print("")
if f_sex == "男" :
print(f"账号{f_id},{f_name} 先生未获得管理员权限。")
else:
print(f"账号{f_id},{f_name} 女士未获得管理员权限。")
print("")
if f_sex == "男" :
cz = int(input(f"若您需要为账号{f_id},{f_name} 先生增添管理员权限请输入1,否则输入0:"))
else:
cz = int(input(f"若您需要为账号{f_id},{f_name} 女士增添管理员权限请输入1,否则输入0:"))
if cz == 0 :
clear()
print("--------------------增添权限--------------------")
print("")
print("3秒后自动返回管理员系统主页面。")
time.sleep(3)
g_main_view()
else:
i[10] = 0
f_level = 0
clear()
print("--------------------增添权限--------------------")
print("")
jdt("增添中,请稍等",0.03)
clear()
print("--------------------增添权限-------------------")
print("")
if f_sex == "男" :
print(f"您已为账号{f_id},{f_name} 先生增添管理员权限。")
else:
print(f"您已为账号{f_id},{f_name} 女士增添管理员权限。")
with open("user_data.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
line = line.strip() # 去掉换行符
words = line.split(",") # 按照","进行分割
# print(words)
list3.append(int(words[1]))
list1 = list({}.fromkeys(list3).keys()) # 对list1进行去重
list2.append(words[4])
all_list.append(words)
if list1.index(f_id) == (len(list1) - 1):
file[list1.index(f_id)] = f"{f_name},{f_id},{f_password},{f_sex},{f_sfz},{f_money},{f_limit},{f_year},{f_date},{f_phone},{f_level}"
else:
file[list1.index(f_id)] = f"{f_name},{f_id},{f_password},{f_sex},{f_sfz},{f_money},{f_limit},{f_year},{f_date},{f_phone},{f_level}\n"
with open("user_data.txt", "w+", encoding="utf-8") as f:
f.writelines(file)
f.flush()
time.sleep(3)
g_main_view()
else:
# print("--------------------权限管理--------------------")
# print("")
print("该账户已注销。")
time.sleep(3)
clear()
g_main_view()
else:
print("您输入的账号有误,请查证后重新输入。")
time.sleep(3)
clear()
qxgl()
elif int(cz) == 2 :
print("--------------------移除权限--------------------")
print("")
f_id = int(input("请输入待服务账号:"))
print("")
if f_id in list1:
for i in all_list:
if f_id == int(i[1]):
f_name = i[0]
f_password = int(i[2])
f_sex = i[3]
f_sfz = i[4]
f_money = int(i[5])
f_limit = int(i[6])
f_year = int(i[7])
f_date = i[8]
f_phone = int(i[9])
f_level = int(i[10])
if f_level == 1:
clear()
print("--------------------移除权限--------------------")
print("")
if f_sex == "男" :
print(f"账号{f_id},{f_name} 先生未获管理员权限。")
else:
print(f"账号{f_id},{f_name} 女士未获管理员权限。")
time.sleep(3)
g_main_view()
else:
clear()
print("--------------------移除权限--------------------")
print("")
if f_sex == "男" :
print(f"账号{f_id},{f_name} 先生拥有管理员权限。")
else:
print(f"账号{f_id},{f_name} 女士拥有管理员权限。")
print("")
if f_sex == "男" :
cz = int(input(f"若您需要移除账号{f_id},{f_name} 先生的管理员权限请输入1,否则输入0:"))
else:
cz = int(input(f"若您需要移除账号{f_id},{f_name} 女士的管理员权限请输入1,否则输入0:"))
if cz == 0:
clear()
print("--------------------移除权限--------------------")
print("")
print("3秒后自动返回管理员系统主页面。")
time.sleep(3)
g_main_view()
else:
i[10] = 1
f_level = 1
# print(f_level)
clear()
print("--------------------移除权限--------------------")
print("")
jdt("移除中,请稍等", 0.03)
clear()
print("--------------------移除权限--------------------")
print("")
if f_sex == "男" :
print(f"您已移除账号{f_id},{f_name} 先生的管理员权限。")
else:
print(f"您已移除账号{f_id},{f_name} 女士的管理员权限。")
with open("user_data.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
line = line.strip() # 去掉换行符
words = line.split(",") # 按照","进行分割
# print(words)
list3.append(int(words[1]))
list1 = list({}.fromkeys(list3).keys()) # 对list1进行去重
list2.append(words[4])
all_list.append(words)
if list1.index(f_id) == (len(list1) - 1):
# print("yes")
file[list1.index(
f_id)] = f"{f_name},{f_id},{f_password},{f_sex},{f_sfz},{f_money},{f_limit},{f_year},{f_date},{f_phone},{f_level}"
else:
# print("no")
file[list1.index(
f_id)] = f"{f_name},{f_id},{f_password},{f_sex},{f_sfz},{f_money},{f_limit},{f_year},{f_date},{f_phone},{f_level}\n"
with open("user_data.txt", "w+", encoding="utf-8") as f:
f.writelines(file)
f.flush()
time.sleep(3)
g_main_view()
else:
print("您输入的账号有误,请查证后重新输入。")
time.sleep(3)
qxgl()
else:
print("您的输入有误,请重新输入。")
time.sleep(3)
clear()
qxgl()
# 删除账户
def del_data() :
global list1
global f_money
global f_limit
global f_password
global f_id
global f_name
global f_sex
global f_sfz
global f_year
global f_date
global f_phone
global f_level
clear()
print("--------------------删除账户--------------------")
print("")
f_id = int(input("请输入待服务账号:"))
print("")
if f_id in list1:
for i in all_list:
if f_id == int(i[1]):
f_name = i[0]
# print(f_name)
f_password = int(i[2])
# print(f_password)
f_sex = i[3]
f_sfz = i[4]
f_money = int(i[5])
f_limit = int(i[6])
f_year = int(i[7])
f_date = i[8]
f_phone = int(i[9])
f_level = int(i[10])
# print(f_level)
if int(f_level) == int(2) :
if f_sex == "男":
print(f"账号{f_id},{f_name} 先生已销户。")
time.sleep(3)
clear()
g_main_view()
else:
print(f"账号{f_id},{f_name} 女士已销户。")
time.sleep(3)
clear()
g_main_view()
else:
s_f_sfz = input("请输入客户身份证号:")
if s_f_sfz == f_sfz :
i[10] = 2
f_level = 2
i[4] = "销户" + i[4]
f_sfz = "销户" + f_sfz
clear()
print("--------------------删除账户-------------------")
print("")
jdt("删除账户中,请稍等", 0.03)
clear()
print("--------------------删除账户-------------------")
print("")
if f_sex == "男" :
print(f"账号{f_id},{f_name} 先生已成功销户。")
else:
print(f"账号{f_id},{f_name} 女士已成功销户。")
with open("user_data.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
line = line.strip() # 去掉换行符
words = line.split(",") # 按照","进行分割
# print(words)
list3.append(int(words[1]))
list1 = list({}.fromkeys(list3).keys()) # 对list1进行去重
list2.append(words[4])
all_list.append(words)
if list1.index(f_id) == (len(list1) - 1):
# print("yes")
file[list1.index(f_id)] = f"{f_name},{f_id},{f_password},{f_sex},{f_sfz},{f_money},{f_limit},{f_year},{f_date},{f_phone},{f_level}"
else:
# print("no")
file[list1.index(f_id)] = f"{f_name},{f_id},{f_password},{f_sex},{f_sfz},{f_money},{f_limit},{f_year},{f_date},{f_phone},{f_level}\n"
with open("user_data.txt", "w+", encoding="utf-8") as f:
f.writelines(file)
f.flush()
time.sleep(3)
g_main_view()
else:
print("客户身份证输入有误,请查证后重新输入。")
time.sleep(3)
clear()
del_data()
技术细节
# 打开保存用户信息的txt文件
def read_txt() :
global list1
global list2
global all_list
all_list = [] # 把txt文件中的每一行都作为一个列表,把所有列表嵌套为一个列表
list1 = [] # 记录txt文件中得到id
list2 = [] # 记录txt文件中的sfz
with open("user_data.txt", "r", encoding="utf-8") as f:
for line in f.readlines():
line = line.strip() # 去掉换行符
words = line.split(",") # 按照","进行分割
# print(words)
list3.append(int(words[1]))
list1 = list({}.fromkeys(list3).keys())
list2.append(words[4])
all_list.append(words)
# 每次更新完值后修改txt文件的函数
def update_txt() :
global list1
with open("user_data.txt", "r", encoding="utf-8") as f:
file = f.readlines()
for line in file:
line = line.strip() # 去掉换行符
words = line.split(",") # 按照","进行分割
# print(words)
list3.append(int(words[1]))
list1 = list({}.fromkeys(list3).keys()) # 对list1进行去重
list2.append(words[4])
all_list.append(words)
if list1.index(c_id) == (len(list1) - 1):
# print("yes")
file[list1.index(c_id)] = f"{c_name},{c_id},{c_password},{c_sex},{c_sfz},{c_money},{c_limit},{c_year},{c_date},{c_phone},{c_level}"
else:
# print("no")
file[list1.index(c_id)] = f"{c_name},{c_id},{c_password},{c_sex},{c_sfz},{c_money},{c_limit},{c_year},{c_date},{c_phone},{c_level}\n"
with open("user_data.txt", "w+", encoding="utf-8") as f:
f.writelines(file)
f.flush()
实现效果
分别是姓名、卡号、密码、性别、身份证号、余额、日限额、出生年份、出生月日、手机号、权限等级(0–管理员、1–普通用户、2–已销户用户)
小结
整个程序写完是1539行代码,能够帮助Python学习者巩固所学知识,也能作为一个练手的程序来自己进行复现,也能作为大作业或者设计,代码量是足够的。运行代码时一定要保证相同文件夹下user_data.txt文件是存在的,否则将会报错。
如需要源代码,请联系原作者,或添加微信:mohuang0114。