zoukankan      html  css  js  c++  java
  • emmm......就当练习了系列09

    1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改

    def file(filename, old, new):
    import os
    with open(filename, 'r', encoding='utf-8') as read_f,
    open('啦啦啦.swap', 'w', encoding='utf-8') as write_f:
    for line in read_f:
    if old in line:
    line = line.replace(old, new)
    write_f.write(line)
    os.remove(filename)
    os.rename('啦啦啦.swap', filename)

    file('C:UserslilingruDesktopLNHday02/h.txt','lucky','NB')

     

    2、编写tail工具

    import time
    def tail():
    with open("log.txt","rb") as f:
    f.seek(0,2)
    while True:
    new_msg = f.readline()
    if len(new_msg)==0:
    time.sleep(1)
    else:
    print(new_msg.decode("utf-8"))
    tail()

    #另一个文件
    def write_log():
    with open("log.txt","a",encoding="utf-8") as f:
    msg = input("请输入日志内容")
    f.write(f" {msg}")
    write_log()


    3、编写登录功能

    def login():
    user_inp = input("your name >")
    pwd_inp = input("your msg >")
    with open("a.txt","r",encoding="utf-8") as f:
    for line in f :
    username,password = line.strip().split(":")
    if username==user_inp and pwd_inp == password:
    print("登录成功")
    break
    else:
    print("输入错误")
    login()


    4、编写注册功能

    def register():
    username = input("请输入注册的用户名:")
    password = input("请输入注册的密码:")
    with open("a.txt","a",encoding="utf-8") as f :
    f.write(f" {username}:{password}")
    register()


    5、编写用户认证功能

    def login():
    inp_u = input("用户名:").strip()
    inp_p = input("密码:").strip()
    with open(r'db.txt', 'rt', encoding='utf-8') as f:
    for line in f:
    user, pwd = line.strip().split(':')
    if inp_u == user and inp_p == pwd:
    print("登录成功")
    return True
    else:
    print("用户名密码错误")
    return False
    def check_user(user_check):
    if user_check:
    print("你猜会出现啥╮(╯▽╰)╭")
    else:
    print("请先登录")
    def main():
    user_check = False
    msg = """
    1、登录
    2、你猜会出现啥╮(╯▽╰)╭
    """
    tag = True
    dic = {
    '1': True,
    '2': False
    }
    while tag:
    print(msg)
    num = input("请输入编号:").strip()
    if not num.isdigit() and num not in dic:
    print("必须输入指定编号")
    else:
    if dic[num]:
    user_check = login()
    else:
    check_user(user_check)
    if __name__ == '__main__':
    main()

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

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

    import os
    def recharge():
    ''' 充值功能'''
    name_inp = input("请输入要充值的账号>")
    money_inp = input("请输入要充值的金钱>")
    with open("db.txt","r",encoding="utf-8") as f ,
    open(".db.txt.swap","w",encoding="utf-8") as f1:
    for line in f :
    if name_inp in line :
    name,money = line.strip().split(":")
    new_money = int(money_inp)+int(money)
    f1.write(line.replace(money,str(new_money)))
    else:
    f1.write(line)
    os.remove("db.txt")
    os.rename(".db.txt.swap","db.txt")

    def transfer():
    '''转账功能'''
    transfer_name_inp = input("请输入转账人姓名:")
    collector_name_inp = input("请输入收账人姓名:")
    money_inp = input("请输入转账金额")
    with open("db.txt","r",encoding="utf-8") as f ,
    open(".db.txt.swap","w",encoding="utf-8") as f1:
    for line in f :
    if transfer_name_inp in line :
    transfer_name,transfer_money=line.strip().split(":")
    new_money = int(transfer_money)-int(money_inp)
    f1.write(line.replace(transfer_money,str(new_money)))
    elif collector_name_inp in line:
    collector_name,collector_money=line.strip().split(":")
    new_money = int(collector_money)+int(money_inp)
    f1.write(line.replace(collector_money,str(new_money)))
    else:
    f1.write(line)
    os.remove("db.txt")
    os.rename(".db.txt.swap","db.txt")
    def cash_withdrawal():
    '''提现功能'''
    name_inp = input("请输入提现账号的姓名:")
    money_inp = input("请输入提现的金额:")
    with open("db.txt","r",encoding="utf-8") as f ,
    open(".db.txt.swap","w",encoding="utf-8") as f1:
    for line in f:
    if name_inp in line:
    name,money = line.strip().split(":")
    new_money = int(money)-int(money_inp)
    f1.write(line.replace(money,str(new_money)))
    else:
    f1.write(line)
    os.remove("db.txt")
    os.rename(".db.txt.swap","db.txt")

    def login():
    '''登录功能'''
    user_inp = input("your name >")
    pwd_inp = input("your msg >")
    with open("a.txt","r",encoding="utf-8") as f:
    for line in f :
    username,password = line.strip().split(":")
    if username==user_inp and pwd_inp == password:
    print("登录成功")
    return 1
    else:
    print("输入错误")

    def query():
    '''查询功能'''
    name_inp=input("请输入你要查询的用户名:")
    with open("db.txt","r",encoding="utf-8")as f :
    for line in f :
    name,money = line.strip().split(":")
    if name_inp in line:
    print(money)
    break

    #主程序
    list1=["1","2","3","4"]
    while True:
    res = login()
    if res:
    while True:
    print('''
    欢迎来到只能充值转账提现的劣质ATM系统
    1.充值
    2.转账
    3.提现
    4.查询
    ''')
    cmd = input("请输入指令>")
    if cmd ==list1[0]:
    recharge()
    elif cmd == list1[1]:
    transfer()
    elif cmd == list1[2]:
    cash_withdrawal()
    elif cmd == list1[3]:
    query()
    else:
    print("非法输入")
  • 相关阅读:
    终端设备 tty,pty,pts 概念与文件描述符的联系
    Nginx – access_log格式及配置
    Nginx – rewrite 配置 URL重写及301跳转原理图
    nginx命令启动及选项
    nginx-web身份验证
    nginx_server_location对客户资源的辨别规则
    利用Session防止表单重复提交
    归并排序
    Cookie/Session的机制与安全
    HTTP Cookie Session
  • 原文地址:https://www.cnblogs.com/lucky-cat233/p/12512676.html
Copyright © 2011-2022 走看看