作业

2020-6-15

作业

1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def edit_file(src, old_content, new_content):
    import os
    if os.path.isfile(src):
        with open(src, mode='rt', encoding='utf-8') as f:
            f = f.read()
            data = f.replace(old_content, new_content)
        with open(src, mode='wt', encoding='utf-8') as f:
            f.write(data)
    else:
        print('文件不存在!')
2、编写tail工具
def tailf_ (file):
    import os, time
    if os.path.isfile(file):
        with open(file, mode='rb') as f:
            while True:
                f.seek(0, 2)
                line = f.readline()
                if line:
                    print(line.decode('utf-8'))

tailf_("/var/log/messages")
              
3、编写登录功能
def read_db():
    '''
    这个是用来读取文件数据库
    '''
    l = []
    with open(r'db.txt', mode='rt', encoding='utf-8') as f:
        print(f)
        for line in f:
            name, pwd = line.strip('\n').split(':')
            l.append(name)
    return l


def login():
    '''
    这个是登录程序
    '''
    name = input('请输入你的用户名:')
    pwd = input("请输入你的密码:")
    if name not in read_db():
        print('你输入的用户名不存在!dSB一个')
    else:
        with open(r'db.txt', mode='rt', encoding='utf-8') as f:
            for line in f:
                name_, pwd_ = line.strip('\n').split(':')
                if name == name_ and pwd == pwd_:
                    print('登录成功')
                    return 0
            else:
                print('密码错误!')

4、编写注册功能

def read_db():
    '''
    读取数据库
    '''
    l = []
    with open(r'db.txt', mode='rt', encoding='utf-8') as f:
        print(f)
        for line in f:
            name, _ = line.strip('\n').split(':')
            l.append(name)
    return l


def register():
    '''
    注册用户
    '''
    name_f = input('请输入你的账户:').strip('}{|\\\t\n.*&^!@#$% ')
    pwd_f = input("请输入你的密码:").strip('}{|\\\t\n.*&^!@#$% ')
    name_n = input('请再次输入你的账户:').strip('}{|\\\t\n.*&^!@#$% ')
    pwd_n = input("请再次输入你的密码:").strip('}{|\\\t\n.*&^!@#$% ')
    res = read_db()
    print(res)
    if name_f in res:
        print('此用户被注册过')

    else:
        if name_f == name_n and pwd_f == pwd_n:
            with open(r'db.txt', mode='at', encoding="utf-8") as f:
                f.write('%s:%s\n' % (name_f, pwd_f))
                print("注册成功")
        else:
            print("注册失败!dsb一个")

5、编写用户认证功能

def auth():
	inp_name = input('请输入你的用户名:')
	inp_pwd = input("请输入你的密码:")
	with open(r'db.txt',mode='rt', encoding='utf-8') as f:
		for line in f:
			name,pwd=line.strip('\n').split(':')
			if name == inp_name and pwd == inp_pwd:
				print("用户登录成功")
				retun 'sucess'
		else:
        	pirnt('用户名或则密码错误!')
这个题目与第三题没区别吧? [<>V<>]

选做题:编写ATM程序实现下述功能,数据来源于文件db.txt

1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改

2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱

3、提现功能:用户输入提现金额,db.txt中该账号钱数减少

4、查询余额功能:输入账号查询余额

选做题中的选做题:登录功能

用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作

def read_db():
    '''
    这个是用来读取文件数据库
    '''
    l = []
    with open(r'db.txt', mode='rt', encoding='utf-8') as f:
        print(f)
        for line in f:
            name, pwd = line.strip('\n').split(':')
            l.append(name)
    return l


def login():
    '''
    这个是登录程序
    '''
    name = input('请输入你的用户名:')
    pwd = input("请输入你的密码:")
    if name not in read_db():
        print('你输入的用户名不存在!dSB一个')
    else:
        with open(r'db.txt', mode='rt', encoding='utf-8') as f:
            for line in f:
                name_, pwd_ = line.strip('\n').split(':')
                if name == name_ and pwd == pwd_:
                    print('登录成功')
                    return 0, name
            else:
                print('密码错误!')
                return 'error', 101


def chaxun(name):  # 查询功能
    with open(r'info.txt',encoding='utf-8',mode='rt') as f:
        for line in f:
            # line = line.strip()
            # print(len(line))
            try:
                data = eval(line)
            except SyntaxError:
                pass
            if data.get(name,0):
                # print(type(data))
                msg = """
                用户:{0}
                余额:{1}
                """.format(name, data[name])
                print(msg)
# chaxun('egon')

def chongzhi(name): # 充值功能
    chaxun(name)
    money = int(input("请充值多少钱:"))
    import os,time
    with open(r'info.txt',encoding='utf-8',mode='rt') as f_r,open(r'.info.txt',encoding='utf-8',mode='wt') as f_w:
        for line in f_r:
            try:
                r_data = eval(line)
            except SyntaxError:
                pass
            if name and r_data.get(name, 0):
                r_data[name] += money
                str_data = str(r_data)
                line = line.replace(line,str_data)
            f_w.write(line+'\n')
        f_r.close()  # 这个写的有点bug如果不加上就会产生文件占用问题
        f_w.close()
        time.sleep(2)
        os.remove('info.txt')
        os.rename('.info.txt','info.txt')
        chaxun(name)

# chongzhi('egon')
#

def tixian(name):
    chaxun(name)
    money = int(input("提现多少钱:"))
    import os,time
    with open(r'info.txt',encoding='utf-8',mode='rt') as f_r,open(r'.info.txt',encoding='utf-8',mode='wt') as f_w:
        for line in f_r:
            try:
                r_data = eval(line)
            except SyntaxError:
                pass
            if name and r_data.get(name, 0):
                if r_data[name] < money:
                    r_data[name] -= money
                    str_data = str(r_data)
                    line = line.replace(line,str_data)
                    f_w.write(line+'\n')
                else:
                    pass
                    # f_w.write(line)
        f_r.close()  # 这个写的有点bug如果不加上就会产生文件占用问题
        f_w.close()
        time.sleep(2)
        os.remove('info.txt')
        os.rename('.info.txt','info.txt')
        chaxun(name)



res, name = login()
print(res)
if res == 0:
    flag = True
    while flag:
        msg = """
        下面是银行操作:请输入数字
        1:充值功能
        2:转账功能
        3:提现功能
        4:查询功能
        5:退出银行
        """
        print(msg)
        res = input('请输入数字:').strip('')
        if not res.isdigit():
            continue

        elif int(res) == 1:
            chongzhi(name)
            # chaxun(name)
        elif int(res) == 2:
            pass
        elif int(res) == 3:
            tixian(name)
        elif int(res) == 4:
            chaxun(name)
        elif int(res) == 5:
            flag=False
            break

else:
    print('密码或者用户名错误')

posted @ 2020-06-15 22:29  为了等  阅读(119)  评论(0编辑  收藏  举报