day12作业

帅兰

# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
# 丑的太自然
def func(x, y, z):
    with open(x, mode='r+t', encoding='utf-8')as f:
        a = f.read()
        b = a.replace(y,z)
    with open(x, mode='wt', encoding='utf-8')as f1:
        f1.write(b)


func('a.txt', '丑', '帅')
# 帅的太自然


import time

while True:
    with open('access.log',mode='at',encoding='utf-8')as f:
        times = time.strftime("%Y-%m-%d %H:%M:%S")
        msg = f'位于{times}帅兰又变帅了\n'
        f.write(msg)
        time.sleep(5)

import time


with open('access.log', mode='rb') as f:
    f.seek(0, 2)
    while True:
        line = f.readline()
        if len(line) == 0:
            time.sleep(1)
        else:
            print(line.decode('utf-8'), end='')

# 2、编写tail工具
import time

def func(x):
    with open(x, mode='rb') as f:
        f.seek(0, 2)
        while True:
            line = f.readline()
            if len(line) == 0:
                time.sleep(1)
            else:
                print(line.decode('utf-8'), end='')

func('access.log')

def func1(x):
    while True:
        with open(x, mode='at', encoding='utf-8')as f1:
            times = time.strftime("%Y-%m-%d %H:%M:%S")
            mag = f'位于{times}帅兰又变高了\n'
            f1.write(mag)
            time.sleep(2)


func1('access.log')




# 3、编写登录功能

# 4、编写注册功能

# 5、编写用户认证功能
def homepage():
    while True:
        print('''欢迎来到我的认证程序
        1、登入
        2、注册
        3、退出''')
        res = input('请输入指令:').strip()
        if res == '1':
            login()
        elif res == '2':
            regster()
        elif res == '3':
            print('已退出')
            return
        else:
            print('请输入正确指令')


def login():
    while True:
        name = input('请输入用户名:').strip()
        pwd = input('请输入密码:').strip()
        with open('a.txt', mode='rt', encoding='utf-8')as f:
            for i in f:
                u, p = i.strip().split(':')
                if u == name and p == pwd:
                    while True:
                        print('''登入成功
                        请选择服务:

                        1、退出''')
                        z = input('请输入指令').strip()
                        if z == '1':
                            print('已退回主界面')
                            homepage()
                        else:
                            print('请输入正确指令')
            else:
                print('账号密码错误')


def regster():
    print('注册功能')
    name1 = input('请输入用户名:')
    pwd1 = input('请输入密码:')
    with open('a.txt', mode='at', encoding='utf-8') as f1:
        f1.write(f'{name1}:{pwd1}\n')

        print('注册成功自动返回主界面')
    homepage()





# 作业后的作业: 判断商品若存在,则合并总价与购买数量
msg_dic = {
    'apple': 10,
    'tesla': 100000,
    'mac': 3000,
    'lenovo': 30000,
    'chicken': 10,
}
dic = {}
tag = True
while tag:
    wares = input('请输入商品名:').strip()
    for i in msg_dic:
        if wares == i:
            count = input('请输入购买个数:').strip()
            if count.isdigit():
                count = int(count)
                price = count * msg_dic[i]
                print('购买成功信息如下', wares, '个数:', count, '价格:', price)
                if wares in dic:
                    dic[wares][0] += count
                    dic[wares][1] += price
                else:
                    dic.setdefault(wares, [count, price])
                print('总数', dic)
                break
            else:
                print('请输入正确的数字')
                break
    else:
        print('请输入正确的商品名称')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值