1. 程式人生 > >python-code-03

python-code-03

[] 變長參數 int 信息 pen 數字 例如 條件 取出

函數作業:
1、復習函數參數的使用
2、實現如下功能
編寫用戶註冊函數,實現功能
1、在函數內接收用戶輸入的用戶名、密碼、余額
要求用戶輸入的用戶名必須為字符串,並且保證用戶輸入的用戶名不與其他用戶重復
要求用戶輸入兩次密碼,確認輸入一致
要求用戶輸入的余額必須為數字
2、要求註冊的用戶信息全部存放於文件中
技術分享圖片
import os


def read_from_db(name):
    """
    從文件中讀數據
    :return:
    """
    is_exist = os.path.exists(
db.txt) # print(is_exist) if is_exist: with open(db.txt, r) as f: content = f.read() if name in content: print(該用戶已存在!) return False else: return else: # 創建文件 with open(db.txt
, w): pass # return True def save_to_db(*args, **kwargs): print(args) """ 將數據保存到數據庫 :return: """ with open(db.txt, a) as f: f.write({},{},{}\n.format(*args, **kwargs)) msg = 註冊成功 return msg def register(): """ 註冊功能 :return:
""" while 1: name = input(>>請輸入用戶名>>) # 判斷輸入值是否合法 if not name.isalpha(): print(用戶名非法) continue # 用戶是否已經註冊過 name_exist = read_from_db(name) if name_exist: pwd = input(>>請輸入密碼>>) re_pwd = input(>>請確認密碼>>) if pwd != re_pwd: print(兩次密碼不一致!) continue balance = input(>>請輸入余額>>) if not balance.isdigit(): print(輸入余額非法!) continue msg = save_to_db(name, pwd, balance) print(msg) else: continue if __name__ == __main__: register() # read_from_db() # save_to_db(‘c‘, 1, 1)
View Code

編寫用戶轉賬函數,實現功能
1、傳入源賬戶名(保證必須為str)、目標賬戶名(保證必須為str)、轉賬金額(保證必須為數字)
2、實現源賬戶減錢,目標賬戶加錢
技術分享圖片
def func_transfer():
    import os
    tag = True
    #取出當前文件內所有的用戶名,用於後面判斷賬號名是否存在
    line_name = []
    with open(db, rt, encoding=utf-8) as f_name:
        for line in f_name:
            line = line.strip(\n).split(:)
            line_name.append(line[0])

    while tag:
        #驗證轉出賬號名的合法性
        name_s = input(轉出賬戶名>>: ).strip()
        if not name_s.isalpha():
            print(必須為純字母)
            continue
        if name_s not in line_name:
            print(轉出賬戶名不存在)
            continue
        #取出此賬號名轉賬前的賬號余額,用於後面判斷後面轉賬金額是否足夠
        with open(db,rt,encoding=utf-8) as f_b:
            for line in f_b:
                line = line.strip(\n).split(:)
                if name_s == line[0]:
                    balance = line[2]
                    balance = int(balance)
        print(當前余額:%s %balance)
        while tag:
            #驗證轉入賬號名的合法性
            name_d = input(轉入賬戶名>>: )
            if not name_d.isalpha():
                print(必須為純字母)
                continue
            if name_d not in line_name:
                print(轉出賬戶名不存在)
                continue
            while tag:
                #驗證轉賬金額是否充足
                transfer_amount = input(轉賬金額>>: )
                if not transfer_amount.isdigit():
                    print(轉賬金額必須為整數)
                    continue
                transfer_amount = int(transfer_amount)
                if transfer_amount > balance:
                    print(余額不足,從新輸入)
                    continue
                #上面的條件都符合,則修改文件
                with open(db,rt,encoding=utf-8) as read_f,                        open(db.swap,wt,encoding=utf-8) as write_f:
                    for line in read_f:
                        line = line.strip(\n).split(:)
                        if name_s == line[0]:
                            line[2] = int(line[2]) - transfer_amount
                            line[2] = str(line[2])
                        if name_d == line[0]:
                            line[2] = int(line[2]) + transfer_amount
                            line[2] = str(line[2])
                        line_new = :.join(line)
                        line_new = line_new +\n
                        write_f.write(line_new)
                os.remove(db)
                os.rename(db.swap,db)
                print(轉賬完成)
                tag = False
func_transfer()
View Code

編寫用戶驗證函數,實現功能
1、用戶輸入賬號,密碼,然後與文件中存放的賬號密碼驗證
2、同一賬號輸錯密碼三次則鎖定

3、這一項為選做功能:鎖定的賬號,在五分鐘內無法再次登錄
提示:一旦用戶鎖定,則將用戶名與當前時間寫入文件,例如: egon:1522134383.29839
實現方式如下:

import time

current_time=time.time()
current_time=str(current_time) #當前的時間是浮點數,要存放於文件,需要轉成字符串
lock_user=‘%s:%s\n‘ %(‘egon‘,current_time)

然後打開文件
f.write(lock_user)

以後再次執行用戶驗證功能,先判斷用戶輸入的用戶名是否是鎖定的用戶,如果是,再用當前時間time.time()減去鎖定的用戶名後
的時間,如果得出的結果小於300秒,則直接終止函數,無法認證,否則就從文件中清除鎖定的用戶信息,並允許用戶進行認證
技術分享圖片
import time
import os

name_info = []
with open(db,rt,encoding=utf-8) as f0:
    for line0 in f0:
        line0 = line0.strip(\n).split(:)
        name_info.append(line0[0])
# print(name_info)

lock_users = []
with open(db_lock,rt,encoding=utf-8) as f_lock:
    for line1 in f_lock:
        line1 = line1.strip(\n).split(:)
        lock_users.append(line1[0])
# print(lock_users)

tag = True
while tag:

    name_inp = input(username>>: ).strip()
    if name_inp not in name_info:
        print(用戶名不存在)
        continue

    if name_inp in lock_users:
        current_time = time.time()
        # print(‘用戶已被鎖定‘)
        with open(db_lock, rt, encoding=utf-8) as f_lock_time:
            for line2 in f_lock_time:
                line2 = line2.strip(\n).split(:)
                if name_inp == line2[0]:
                    name_lock_time = line2[1]
                    name_lock_time = float(name_lock_time)
                    # print(name_lock_time,type(name_lock_time))
        valid_time = current_time - name_lock_time
        #時間戳差值轉為秒
        if valid_time < 300:
            print(鎖定狀態)
            tag = False
        else:
            with open(db_lock,rt,encoding=utf-8) as f3,                    open(db_lock.swap,wt,encoding=utf-8) as f4:
                for line3 in f3:
                    line3_new = line3.strip(\n).split(:)
                    if name_inp != line3[0]:
                        f4.write(line3)
            os.remove(db_lock)
            os.rename(db_lock.swap,db_lock)

    with open(db, rt, encoding=utf-8) as f1:
        for line in f1:
            line = line.strip(\n).split(:)
            if name_inp == line[0]:
                name_pwd = line[1]
                break

    count = 1
    while count <= 3:
        pwd_inp = input(password>>: )
        if pwd_inp == name_pwd:
            print(驗證成功)
            tag = False
            break
        else:
            print(密碼錯誤)
            count += 1
            if count == 4:
                current_time = time.time()
                current_time = str(current_time)
                lock_user = %s:%s\n % (name_inp, current_time)
                with open(db_lock,at,encoding=utf-8) as f2:
                    f2.write(lock_user)
                print(%s 用戶已被鎖定五分鐘 %name_inp)
                tag = False
View Code

明日默寫:
1、什麽是形參?什麽是實參?形參與實參的關系是什麽?
技術分享圖片
形參:在函數定義階段括號內定義的參數,稱之為形式參數,簡稱形參,本質就是變量名
實參:在函數調用階段括號內傳入的值,稱之為實際參數,簡稱實參,本質就是變量的值
形參即變量名,實參即變量值,函數調用時,將值綁定到變量名上,函數調用結束,解除綁定
View Code

2、介紹所有形參與實參及其特點
技術分享圖片
1、位置參數:按照從左到右的順序定義的參數
        位置形參:必選參數
        位置實參:按照位置給形參傳值
2、關鍵字參數:按照key=value的形式定義的實參
        無需按照位置為形參傳值
        註意的問題:
                1. 關鍵字實參必須在位置實參右面
                2. 對同一個形參不能重復傳值
3、默認參數:形參在定義時就已經為其賦值
        可以傳值也可以不傳值,經常需要變得參數定義成位置形參,變化較小的參數定義成默認參數(形參)
        註意的問題:
                1. 只在定義時賦值一次
                2. 默認參數的定義應該在位置形參右面
                3. 默認參數通常應該定義成不可變類型
4、可變長參數:
        可變長指的是實參值的個數不固定
        而實參有按位置和按關鍵字兩種形式定義,針對這兩種形式的可變長,形參對應有兩種解決方案來完整地存放它們,分別是*args,**kwargs
5、命名關鍵字參數:*後定義的參數,必須被傳值(有默認值的除外),且必須按照關鍵字實參的形式傳遞
可以保證,傳入的參數中一定包含某些關鍵字
View Code

python-code-03