选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
import os
def invest():
invest_user = input("请输入充值的账号:")
while 1:
invest_money = input("请输入充值多少钱:")
if int(invest_money) > 0:
break
else:
print("金额错误,请重新输入")
invest_tag = 0
with open("db.txt",mode="r",encoding="utf-8") as f1,
open("db.txt.swap",mode="w",encoding="utf-8") as f2:
for line in f1:
user,balance = line.strip().split(":")
if invest_user == user:
invest_tag = 1
balance = float(balance)
balance += float(invest_money)
balance = str(balance)
f2.write('{}:{}
'.format(user,balance))
if invest_tag == 0:
print("充值失败,请检查充值账号是否正确")
elif invest_tag == 1:
print("充值成功")
os.remove("db.txt")
os.rename("db.txt.swap","db.txt")
# invest()
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
import os
def transfer():
transfer_user1 = input("请输入扣钱的账号:")
transfer_user2 = input("请输入充值的账号:")
while 1:
transfer_money = input("请输入转账数量:")
if transfer_money.isdigit():
transfer_money = int(transfer_money)
if int(transfer_money) > 0:
break
else:
print("金额错误,请重新输入")
else:
print("必须输入数字")
transfer_tag = 0
with open("db.txt",mode="r",encoding="utf-8") as f1:
for line in f1:
user, balance = line.strip().split(":")
if transfer_user1 == user:
transfer_tag += 1
elif transfer_user2 == user:
transfer_tag += 1
if transfer_tag == 2:
with open("db.txt", mode="r", encoding="utf-8") as f1,
open("db.txt.swap", mode="w", encoding="utf-8") as f2:
f1.seek(0, 0)
for line in f1:
user, balance = line.strip().split(":")
if transfer_user1 == user:
balance = str(float(balance)-transfer_money)
elif transfer_user2 == user:
balance = str(float(balance)+transfer_money)
f2.write("{}:{}
".format(user,balance))
print("转账成功")
os.remove("db.txt")
os.rename("db.txt.swap", "db.txt")
else:
print("账号错误,请重新输入")
# transfer()
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
import os
def cash_out():
cash_out_user = input("请输入要提现的账号:")
while 1:
cash_out_money = input("请输入提现多少钱:")
if cash_out_money.isdigit():
if int(cash_out_money) > 0:
break
else:
print("金额错误,请重新输入")
else:
print("必须输入数字")
cash_out_tag = 0
with open("db.txt",mode="r",encoding="utf-8") as f1,
open("db.txt.swap",mode="w",encoding="utf-8") as f2:
for line in f1:
user,balance = line.strip().split(":")
if cash_out_user == user:
if float(balance) > int(cash_out_money):
balance = str(float(balance)- float(cash_out_money))
cash_out_tag = 1
else:
print("余额不足,无法提现")
f2.write('{}:{}
'.format(user,balance))
if cash_out_tag == 0:
print("提现失败,请检查充值账号是否正确")
elif cash_out_tag == 1:
print("提现成功")
os.remove("db.txt")
os.rename("db.txt.swap","db.txt")
# cash_out()
4、查询余额功能:输入账号查询余额
import os
def inquire():
inquire_user = input("请输入要查询余额的账号:")
with open("db.txt","r",encoding="utf-8") as f:
for line in f:
user,money = line.strip().split(":")
if user == inquire_user:
print(money)
break
else:
print("账号不存在")
# inquire()
选做题中的选做题:登录功能
用户登录成功后,内存中记录下该状态,
上述功能以当前登录状态为准,必须先登录才能操作
tag = 1
while tag:
inp_user = input("请输入账号").strip()
with open("user_info3.txt", mode="r", encoding="utf-8") as f1,
open("user_info3_copy.txt", mode="w", encoding="utf-8") as f2:
for line in f1:
user, password, count = line.strip().split(":")
count = int(count)
if inp_user == user:
if count == 3:
print("账号被锁定")
else:
for i in range(3):
inp_psw = input("请输入密码").strip()
if inp_psw == password:
print("登陆成功")
tag = 0
break
count += 1
if count == 3:
print("密码错误三次,账号锁定")
break
else:
print("密码错误三次,账号锁定")
count = "3"
count = str(count)
f2.write("{}:{}:{}
".format(user, password, count))
os.remove('user_info3.txt')
os.rename("user_info3_copy.txt","user_info3.txt")
msg = """
0 退出
1 充值
2 转账
3 提现
4 查询
"""
while 1:
cmd = input("{}".format(msg)).strip()
if cmd.isdigit():
if cmd == "0":
break
elif cmd == "1":
invest()
elif cmd == "2":
transfer()
elif cmd == "3":
cash_out()
elif cmd == "4":
inquire()
else:
print("命令不存在")
else:
print("必须输入数字")