函数的基本使用

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

import os


def func(address, old_concent, new_concent):
    with open(r'{}'.format(address), 'rb') as f, \
            open(r'{}.swap'.format(address), 'wb') as f1:
        for line in f:
            res = line.decode('utf-8').replace(old_concent, new_concent)
            f1.write(bytes(res, encoding='utf-8'))
    os.remove(r'{}'.format(address))
    os.rename('{}.swap'.format(address), '{}'.format(address))


adr = input('请输入地址:')
old = input('请输入需要修改的原文:')
new = input('请输入需要修改的新文:')
func(adr, old, new)

2. 编写tail工具

import time


def input_info():
    with open(r'info.txt', 'ab') as f1:
        res = input('输入要保存的内容:')
        f1.write('{}\n'.format(res).encode('utf-8'))


def tail():
    with open(r'info.txt', 'rb') as f2:
        f2.seek(0, 2)
        while True:
            info = f2.read()
            if len(info) == 0:
                time.sleep(0.5)
            else:
                print(info.decode('utf-8'), end='')


tail()

3. 编写登录功能

def login(account,password):
    with open(r'account.txt','rt',encoding='utf-8') as f1:
        for line in f1:
            acc,pwd = line.strip().split(':')
            if account == acc and pwd == password:
                print('登录成功')
                break
        else:
            print('用户名或密码错误')

acc = input('请输入账号')
pwd = input('请输入密码')
login(acc, pwd)

4. 编写注册功能

def register(account, password):
    with open(r'account.txt', 'a+b') as f:
        f.seek(0, 0)
        for line in f:
            account, password = line.strip().split(':'.encode('utf-8'))
            if acc.encode('utf-8') == account:
                print('账号已存在')
                break
        else:
            f.seek(0, 2)
            f.write('\n{}:{}'.format(acc, pwd).encode('utf-8'))
            print('注册成功')
            # f.write(bytes('\n{}:{}'.format(account, password), encoding='utf-8')

acc = input('请输入账号')
pwd = input('请输入密码')
register(acc, pwd)

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)

6. 编写ATM程序

'''
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额
# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
'''

# 充值功能
import os


# def recharge(account, money):
def recharge():
    account = login_user
    money = input('请输入充值金额:')
    with open(r'db.txt', 'rt', encoding='utf-8') as f1, \
            open(r'db.txt.swap', 'wt', encoding='utf-8') as f2:
        for line in f1:
            acc, money_old = line.strip().split(':')
            if acc == account:
                money_old = int(money_old)
                money = int(money)
                res = line.replace(str(money_old), str(money_old + money))
                f2.write(res)
                print('充值金额为{},余额为{}'.format(money, money + money_old))
            else:
                f2.write(line)
    os.remove(r'db.txt')
    os.rename('db.txt.swap', 'db.txt')


# recharge('A', 500)


#  转账功能
# def transfer_accpunts(account_trans, account_rece, money):
def transfer_accpunts():
    account_trans = login_user
    account_rece = input('请输入接收账号:')
    money = input('请输入转账金额:')
    with open(r'db.txt', 'rt', encoding='utf-8') as f3, \
            open(r'db.txt.swap', 'wt', encoding='utf-8') as f4:
        for line in f3:
            acc, money_old = line.strip().split(':')
            money_old = int(money_old)
            money = int(money)
            if acc == account_trans:
                money_l1 = money_old - money
                res = line.replace(str(money_old), str(money_l1))
                f4.write(res)
            elif acc == account_rece:
                money_l2 = money_old + money
                res = line.replace(str(money_old), str(money_l2))
                f4.write(res)
            else:
                f4.write(line)
        print('{}向{}转账{}元,{}余额为{},{}余额为{}'.format(account_trans, money, account_rece, account_trans, money_l1,
                                                  account_rece, money_l2))
    os.remove('db.txt')
    os.rename('db.txt.swap', 'db.txt')


# transfer_accpunts('B','C',1500)


# 提现功能
# def withdraw(account, money):
def withdraw():
    account = login_user
    money = input('请输入提现金额:')
    with open(r'db.txt', 'rt', encoding='utf-8') as f5, \
            open(r'db.txt.swap', 'wt', encoding='utf-8') as f6:
        for line in f5:
            acc, money_old = line.strip().split(':')
            if acc == account:
                money_old = int(money_old)
                money = int(money)
                res = line.replace(str(money_old), str(money_old - money))
                f6.write(res)
                print('提现金额为{},余额为{}'.format(money, money_old - money))
            else:
                f6.write(line)
    os.remove(r'db.txt')
    os.rename('db.txt.swap', 'db.txt')


# withdraw('A',100)


# 查询余额
def remainder():
    account = login_user
    with open(r'db.txt', 'rt', encoding='utf-8') as f7:
        for line in f7:
            acc, money = line.strip().split(':')
            if acc == account:
                print('余额为{}元'.format(money))


# remainder('A')
func_dic = {
    '0': ('退出', None),
    '1': ('充值', recharge),
    '2': ('转账', transfer_accpunts),
    '3': ('提现', withdraw),
    '4': ('查询', remainder),
}

login_user = None
while True:
    account = input('请输入账号:')
    password = input('请输入密码:')
    with open(r'bb.txt', 'rt', encoding='utf-8') as ff:
        for line in ff:
            acc, pwd = line.strip().split(':')
            if acc == account and pwd == password:
                # global login_user
                login_user = account
                print('{}登陆成功'.format(login_user))
                while True:
                    for i, j in func_dic.items():
                        print('{}:{}'.format(i, j[0]))
                    operate = input('请输入待选事项序号:').strip()
                    if not operate.isdigit():
                        print('请重新输入序号')
                    if operate == '0':
                        break
                    elif operate in func_dic:
                        func_dic[operate][1]()
                    else:
                        print('请重新输入正确序号')
                break
        else:
            print('账号密码错误,请重新输入')
    break
# print('hello')

 

posted @ 2020-12-26 15:37  Avery_W  阅读(57)  评论(0编辑  收藏  举报