欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

2020 7.17

程序员文章站 2022-05-04 19:17:37
# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)# 既可完成文件的修改import osimport timedef change_filepath(file_path, org_txt, new_txt): file_path = "%s\.copy.txt" % file_path with open("new.txt", mode="rt", encoding="utf-8")as f1, \ open...
# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)
# 既可完成文件的修改
import os
import time


def change_filepath(file_path, org_txt, new_txt):
    file_path = "%s\.copy.txt" % file_path
    with open("new.txt", mode="rt", encoding="utf-8")as f1, \
            open(file_path, mode="wt", encoding="utf-8")as f2:
        data = f1.read()
        change_txt = data.replace(org_txt, new_txt)
        f2.write(change_txt)
    os.remove(file_path)
    os.rename(file_path, 'new2.txt')


# change_filepath(file_path=r'E:\Python15期\Day 11 函数', org_txt="2548", new_txt="牛牛牛")

# 2、编写记录tail工具生成


def generate_log():  # 日志生成
    for i in range(1, 9999):
        with open("logs.txt", "at", encoding="utf-8")as f:
            f.write(f"正在记录第{i}次日志记录............\n")
            time.sleep(2)


def record_tail():  # 记录日志
    with open("logs.txt", "rb")as f:
        f.seek(0, 2)
        while True:
            data = f.readline()
            if len(data) == 0:
                pass
            else:
                print(data.decode("utf-8"))
                time.sleep(0.5)


# 3、编写登录功能
def log_in(i):  # i 为重新输入密码的次数
    user_id = input("请输入你的账号:").strip()
    user_psd = input("请输入你的密码:").strip()
    if user_id == "EGON" and user_psd == "123":
        print("登录成功")
    else:
        if i == 0:  # 输入次数达到上限 退出
            print("你已经累计输错密码%s次,账户被锁定!" % 3)
        else:
            print("请输入正确的密码!")
            return log_in(i - 1)


# log_in(3)


# 4、编写注册功能
# 敏感字符
sensitive_character = set()
for i in range(33, 48):
    sensitive_character.add(chr(i))


def sign_up():
    new_id = input("请输入你要注册的用户名:").strip()
    new_psd = input("请输入你要注册的密码:").strip()
    new_psd2 = input("请再次输入你的密码").strip()
    set_newid = set(new_id)
    if new_psd != new_psd2:
        print("两次输入密码不一致,请重新输入")
    else:
        if sensitive_character & set_newid != set():
            print("你注册的用户名不符合规范!")
            sign_up()
        if len(new_psd) < 6:
            print("你输入的密码太短了!")
            sign_up()
        else:
            print("恭喜你登录成功!")
            with open("newuser.txt", mode="at", encoding="utf-8") as f:
                f.write("%s:%s\n" % (new_id, new_psd))


# 5、编写用户认证功能 检验
def tset_id():
    user_name = input("请输入你要检验的账号:")
    names = []
    with open(r"C:\Users\S\Desktop\userdata.txt", "rt", encoding="utf-8") as f:
        for line in f:
            name = line.strip("\n").split(":")[0]
            names.append(name)
    print(names)
    if user_name in names:
        print("用户名已被注册!")
    else:
        print("用户名可用。")


#
# tset_id()
# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额


def atm_function():
    print("""请输入你要进行的操作:
            1:  充值
            2:  转账
            3:  提现
            4:  查询""")
    user = input("请输入你的账号:")
    users = {}
    # 创建db文件并写入数据
    with open("db.txt", "wt", encoding="utf-8")as f1:
        f1.write("A:999999\nB:888888\nC:777777\nD:666666\n")
    # 读取db文件
    with open("db.txt", "rt", encoding="utf-8")as f2:
        for line in f2:
            k, v = line.strip("\n").split(":")
            users[k] = v
    print(users)

    def change_data():  # 改变数据
        with open("db.txt", "wt", encoding="utf-8") as f:
            for k, v in users.items():
                f.write("%s:%s\n" % (k, v))

    change_data()

    cmd = input("请输入你的操作:")
    if cmd == str(1):
        top_up = input("请输入你要充值的金额:").strip()
        users[user] = int(users[user]) + int(top_up)
    elif cmd == str(2):
        transfer_id = input("请输入你要转账的账户:").strip()
        transfer_am = input("请输入你要转账的金额:").strip()
        users[user] = int(users[user]) - int(transfer_am)  # 原用户减少
        users[transfer_id] = int(users[transfer_id]) + int(transfer_am)  # 受益用户
        change_data()
    elif cmd == str(3):
        withdraw = input("请输入你要提现的金额:").strip()
        users[user] = int(users[user]) - int(withdraw)
        change_data()
    else:
        print("请输入正确的操作!")
        atm_function()


# atm_function()


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


def log_in(i):  # i 为重新输入密码的次数
    user_id = input("请输入你的账号:").strip()
    user_psd = input("请输入你的密码:").strip()

    def record_logdata():  # 内建函数
        with open("logdata.txt", "at", encoding="utf-8") as f:
            f.write("%s登陆成功!登陆次数为%s\n" % (user_id, abs(4 - i)))

    if user_id == "EGON" and user_psd == "123":
        print("登录成功")
        record_logdata()
    else:
        if i == 0:  # 输入次数达到上限 退出
            print("你已经累计输错密码%s次,账户被锁定!" % 3)
        else:
            print("请输入正确的密码!")
            return log_in(i - 1)

# log_in(3)

本文地址:https://blog.csdn.net/A1L__/article/details/107427085