作业
import os
if not os.path.exists("D:/流水.txt"):
f = open("D:/流水.txt", 'w')
f.close()
f = open("D:/流水.txt", 'r')
log = f.read()
f.close()
while 1:
a = int(input("请选择操作:1存款 2取款 3查询余额 0结束"))
if a == 0:
f = open("D:/流水.txt", 'w')
f.write(log)
f.close()
print("感谢您的使用")
break
if a == 1:
num = int(input("请输入存入金额"))
if num > 0:
log += "存入 " + str(num) + "\n"
print("存入成功")
elif a == 2:
num = int(input("请输入取出金额"))
if num > 0:
log += "取出 " + str(num) + "\n"
print("取出成功")
elif a == 3:
sum = 0
log_list = log.split("\n")
for line in log_list:
if "存入" in line:
sum += int(line.split()[1])
elif "取出" in line:
sum -= int(line.split()[1])
print("余额为:", sum)