1. 程式人生 > 實用技巧 >那些很努力卻沒有成就的人都有一個特點

那些很努力卻沒有成就的人都有一個特點

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')