1. 程式人生 > 實用技巧 >函式物件與閉包

函式物件與閉包

1. 函式物件優化多分支if的程式碼練熟

def exit():
    print('退出')


def login():
    print('登入功能')


def transfer():
    print('轉賬功能個')


def check_balance():
    print('餘額查詢功能')


def withdraw():
    print('提現功能')


def register():
    print('註冊功能')


func_dic = {
    '1': ('login',login),
    '2': ('transfer',transfer),
    
'3': ('check_balance',check_balance), '4': ('withdraw',withdraw), '5': ('register',register), '0': ('exit',exit), } def main(): for i, j in func_dic.items(): print('{}:{}'.format(i, j[0])) while True: operate = input('請輸入待選事項序號:') if not operate.isdigit():
print('請重新輸入序號') continue if operate in func_dic: func_dic[operate][1]() else: print('請重新輸入正確序號') main()

2. 編寫計數器功能,要求呼叫一次在原有的基礎上加一

def outter_calculate():
    n = 0

    def calculate():
        nonlocal n
        n += 1
        return n
    return
calculate calculate = outter_calculate() print(calculate()) print(calculate()) print(calculate()) print(calculate()) print(calculate())

3. ATM程式

'''
ATM程式
# 編寫ATM程式實現下述功能,資料來源於檔案db.txt
# 0、註冊功能:使用者輸入賬號名、密碼、金額,按照固定的格式存入檔案db.txt
# 1、登入功能:使用者名稱不存在,要求必須先註冊,使用者名稱存在&輸錯三次鎖定,登入成功後記錄下登入狀態(提示:可以使用全域性變數來記錄)

# 下述操作,要求登入後才能操作
# 1、充值功能:使用者輸入充值錢數,db.txt中該賬號錢數完成修改
# 2、轉賬功能:使用者A向用戶B轉賬1000元,db.txt中完成使用者A賬號減錢,使用者B賬號加錢
# 3、提現功能:使用者輸入提現金額,db.txt中該賬號錢數減少
# 4、查詢餘額功能:輸入賬號查詢餘額
'''
import os

login_user = None


#  註冊功能
def register():
    while True:
        account_input = input('請輸入註冊賬號:').strip()
        password_input = input('請輸入密碼:').strip()
        money_input = input('請輸入存款額:').strip()
        with open(r'db.txt', 'rt', encoding='utf-8') as f1, \
                open(r'db.txt', 'at', encoding='utf-8') as f2:
            for line in f1:
                account, password, money = line.strip().split(':')
                if account_input == account:
                    print('賬號已存在,請重新輸入!')
                    break
            else:
                f2.write('\n{}:{}:{}'.format(account_input, password_input, money_input))
                global login_user
                login_user = account_input
                print('賬號註冊成功')
                break


#  登入功能
# login_user = None


def login():
    amount = True
    n = 0
    while amount:
        account_input = input('請輸入賬號:').strip()
        password_input = input('請輸入密碼:').strip()
        with open(r'db.txt', 'rt', encoding='utf-8') as f1, \
                open(r'blacklist.txt', 'rt', encoding='utf-8') as f2:
            for line1 in f2:
                account_black, *_ = line1.strip().split(':')
                if account_black == account_input:
                    print('對不起,該賬號已被鎖定,程式即將退出!')
                    exit()
            else:
                for line2 in f1:
                    account, password, *_ = line2.strip().split(':')
                    if account_input == account:
                        if password_input == password:
                            global login_user
                            login_user = account_input
                            # print('登陸成功')
                            print(login_user)
                            amount = False
                            break
                        elif n == 2:
                            with open(r'blacklist.txt', 'at', encoding='utf-8') as f3:
                                f3.write('{}:{}\n'.format(account_input, password_input))
                            amount = False
                            print('非常抱歉,您已輸錯三次密碼')
                            print('您的賬號已被鎖定')
                            break
                        elif password_input != password:
                            n += 1
                            print('密碼錯誤,請重新輸入!')
                            print(n)
                            break
                else:
                    print('使用者不存在,請先註冊')
                    register()
                    amount = False


# 登入後操作
# 充值功能
def recharge():
    account = login_user
    money_input = input('請輸入充值金額:').strip()
    money_input = int(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 = line.strip().split(':')
            money = int(money)
            if acc == account:
                line = line.replace(str(money), str(money + money_input))
                f2.write(line)
                print('充值金額為{},餘額為{}'.format(money_input, money + money_input))
            else:
                f2.write(line)
    os.remove(r'db.txt')
    os.rename(r'db.txt.swap', r'db.txt')


#  轉賬功能
def transfer():
    account = login_user
    account_receive = input('請輸入接收賬戶:')
    money_input = input('請輸入轉賬金額:')
    money_input = int(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 = line.strip().split(':')
            money = int(money)
            if acc == account:
                line = line.replace(str(money), str(money - money_input))
                f2.write(line)
                print('{}向{}轉賬{}元,{}餘額為{}'.format(account, account_receive, money_input, account, money - money_input))
            elif acc == account_receive:
                line = line.replace(str(money), str(money + money_input))
                f2.write(line)
                print('{}餘額為{}'.format(account_receive, money + money_input))
            else:
                f2.write(line)

    os.remove(r'db.txt')
    os.rename(r'db.txt.swap', r'db.txt')


# 提現功能
def withdraw():
    account = login_user
    money_input = input('請輸入提現金額:')
    money_input = int(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 = line.strip().split(':')
            money = int(money)
            if acc == account:
                line = line.replace(str(money), str(money - money_input))
                print(money - money_input)
                f2.write(line)
                print('提現金額為{},餘額為{}'.format(money_input, money - money_input))
            else:
                f2.write(line)

    os.remove(r'db.txt')
    os.rename(r'db.txt.swap', r'db.txt')


# 餘額查詢

def remainder():
    account = login_user
    with open(r'db.txt', 'rt', encoding='utf-8') as f1:
        for line in f1:
            acc, *_, money = line.strip().split(':')
            if acc == account:
                print('餘額為{}元'.format(money))


# 主程式
dic_outer = {
    '1': ('註冊', register),
    '2': ('登入', login),
    '0': ('退出', None)
}

dic_inner = {
    '1': ('充值', recharge),
    '2': ('轉賬', transfer),
    '3': ('提現', withdraw),
    '4': ('查詢餘額', remainder),
    '0': ('退出', None)
}

while True:
    for i, j in dic_outer.items():
        print('{}:{}'.format(i, j[0]))
    choice_out = input('請選擇操作專案').strip()
    if not choice_out.isdigit():
        print('請重新輸入正確數字')
    if choice_out in dic_outer:
        if choice_out == '0':
            exit()
        else:
            dic_outer[choice_out][1]()
            if login_user == None:
                print('程式即將退出')
                break
            # print(login_user)
            while True:
                for i, j in dic_inner.items():
                    # print(login_user)
                    print('{}:{}'.format(i, j[0]))
                choice_in = input('請選擇操作專案').strip()
                if not choice_in.isdigit():
                    print('請重新輸入正確數字')
                if choice_in in dic_inner:
                    if choice_in == '0':
                        exit()
                    else:
                        dic_inner[choice_in][1]()

    else:
        print('數字超限,請重新輸入')