# 1、写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作
'''
import os
def file(file,old,new):
with open(file,mode='rt',encoding='utf-8') as f1,open('files.txt',mode='wt',encoding='utf-8') as f2:
for line in f1:
f2.write(line.replace(old,new))
os.remove(file)
os.rename('files.txt',file)
file('db.txt','tank','alice')
'''
# 2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数
'''
l='tank:123: 10007'
def res(strs):
count1=0
count2=0
count3=0
count4=0
for i in strs:
if i.isdigit():
count1 += 1
elif i.isalpha():
count2 += 1
elif i.isspace():
count3 += 1
else:
count4 += 1
print(count1,count2,count3,count4)
res(l)
'''
# 3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
'''
l='tank:123: 10007'
l1=['tank',123,10007]
l2=('tank',123,10007)
def res(in_put):
if len(in_put) >= 5:
print(True)
else:
print(False)
res(l)
res(l1)
res(l2)
'''
# 4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
'''
l=['tank',123,10007]
def res(lists):
if len(lists) >= 2:
lists=lists[0:2]
print(lists)
res(l)
'''
# 5、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
'''
l2=('tank',123,10007)
l=[]
def res(cmd):
cmd=list(cmd)
for i in range(0,len(cmd)):
if i % 2 == 1:
l.append(cmd.pop(i))
print(l)
res(l2)
'''
# 6、写函数,检查字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
# dic = {"k1": "v1v1", "k2": [11,22,33,44]}
# PS:字典中的value只能是字符串或列表
'''
dic = {"k1": "v1v1", "k2": [11,22,33,44]}
def res(dicts):
for i in dicts:
l=dicts[i]
if len(l) >= 2:
l=l[0:2]
print(l)
res(dic)
'''
# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额
# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
dic={}
msg = {'0':'退出','1': '充值','2': '转账','3': '提现','4':'查询余额'}
def users(file,user_input,pas):
with open(file,mode='rt',encoding='utf-8') as f1:
for line in f1:
user,word = line.strip().split(':')
if user_input == user and pas == word:
print('登录成功!')
tag=True
while tag:
with open('db.txt', mode='rt', encoding='utf-8') as f2:
for line2 in f2:
user2, money = line2.strip().split(':')
dic[user2] = int(money)
msg = "'0':'退出','1': '充值','2': '转账','3': '提现','4':'查询余额'"
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字')
continue
elif cmd not in msg:
print('输入的命令不存在')
continue
else:
if cmd == '0':
break
elif cmd == '1':
incount = input('请输入充值金额:').strip()
incount = int(incount)
user2=user_input
dic[user2] += incount
with open('db.txt', mode='wt', encoding='utf-8') as f3:
for user2, money in dic.items():
f3.write(f'{user2}:{money}
')
continue
elif cmd == '2':
usernameb=input('请输入对方账户名称:')
if usernameb == user_input:
print('您不能给自己转账')
continue
if usernameb not in dic:
print('账户不存在')
continue
else:
outcount = input('请输入转账金额:')
outcount = int(outcount)
user2 = user_input
dic[user2] -= outcount
user2=usernameb
dic[user2] += outcount
with open('db.txt', mode='wt', encoding='utf-8') as f3:
for user2, money in dic.items():
f3.write(f'{user2}:{money}
')
continue
elif cmd == '3':
incount = input('请输入提现金额:').strip()
incount = int(incount)
user2=user_input
dic[user2] -= incount
with open('db.txt', mode='wt', encoding='utf-8') as f3:
for user2, money in dic.items():
f3.write(f'{user2}:{money}
')
continue
elif cmd == '4':
user2=user_input
money=dic[user2]
print(money)
else:
print('登录失败')
users('dbs.txt','tank','123')