python15演習

今日の仕事:

1、函数对象优化多分支if的代码练熟
if user_state:
    list=[0,1,2]
    dic={
'查询余额请输入','0'
'提现请输入','1'
'转账请输入','2'
    }
    while 1:
        print(dic)
        cmd=input('请输入指令:')
        if cmd in list:
            call(int(cmd))
            break
        else:
            print('无效的指令')
            continue
def call(x):
    if x==0:#查看余额
        check_money()
    elif x==1:#提现
        withdraw_deposit()
    elif x==2:#转账
        transfer()
2、编写计数器功能,要求调用一次在原有的基础上加一
    温馨提示:
        I:需要用到的知识点:闭包函数+nonlocal
        II:核心功能如下:
            def counter():
                x+=1
                return x


    要求最终效果类似
        print(couter()) # 1
        print(couter()) # 2
        print(couter()) # 3
        print(couter()) # 4
        print(couter()) # 5

週末の作業
次の関数、db.txtファイルからデータを達成するためにATMのプログラムを書く
0、登録機能を:ユーザは、アカウント名、パスワード、およびdb.txt固定フォーマットファイル内に堆積量入力する
必要、ユーザ名が存在しません:1、ログイン機能をユーザ名&ロックを入力するには失敗した試みがあり、登録する必要があり、(ヒント:あなたは、レコードにグローバル変数を使用することができます)ログイン成功のレコードの後にログインし
、次の操作の後に動作させるために、ログインを要求
:お金の充電量、ユーザの入力を、再充電に、1を口座にお金のdb.txt量が修正完了するために
2を、転送機能:ユーザーB転送千元、ユーザAがユーザBの口座にお金を追加し、保存マネー口座db.txt完了すると、ユーザA
3、現金機能:ユーザーの入力引き出し金額、アカウントの減少db.txt金額
4、残高照会機能:口座残高照会を入力します。

user_state=0
def login():
    list_1=[]
    tag=1
    # while tag:
    with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f1:
        name=input('请输入账户:').split()
        for line in f1:
            username=line.split(':')[0]
            # print(username)
            userpwd=line.split(':')[1]
            count=line.split(':')[2]
            # print(count)
            money=line.split(':')[3]
            if name == username:
                print(name==usernane)
                pwd=input('请输入密码').strip()
                if userpwd==pwd:
                    print('登录成功')
                    user_state=name
                    tag=0
                    break
                elif userpwd!=pwd:
                    print('密码错误')
                    count+=1
                else:
                    ...
            else:
                ...

        else:
            print('用户不存在请注册')
            # register()
    if count == 3:
        print('用户已锁定')
        tag = 0
        with open (r'C:\Users\Administrator\Desktop\db.txt', mode='rt', encoding='utf-8') as f2:
            for line in f2:
                if line.split(':')[0] == user_state:
                    line = f'{username}:{userpwd}:{count}:{money}\n'
                list4.append(line)
        with open(r'C:\Users\Administrator\Desktop\db.txt', mode='wt', encoding='utf-8') as f3:
            f3.write(list4)

def register():#注册功能
    tag=1
    name_exist=0
    while tag:
        with open (r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding="utf-8") as f1:
            username=input('请输入要注册的账户:').strip()
            for line in f1:
                name=line.split(':')[0]
                # print(name)
                if name==username:
                    print('用户已存在')
                    break
                    tag=0
                    name_exist=1
            else:
                userpwd=input('请输入要注册的密码:').strip()
            if name_exist==0:
                with open(r'C:\Users\Administrator\Desktop\db.txt',mode='wt',encoding='utf-8') as f2:
                    f2.write(f'{username}:{userpwd}:0:0\n')
                break
login()
if user_state:
    list=[0,1,2]
    dic={
'查询余额请输入','0'
'提现请输入','1'
'转账请输入','2'
    }
    while 1:
        print(dic)
        cmd=input('请输入指令:')
        if cmd in list:
            call(int(cmd))
            break
        else:
            print('无效的指令')
            continue
def call(x):
    if x==0:#查看余额
        check_money()
    elif x==1:#提现
        withdraw_deposit()
    elif x==2:#转账
        transfer()


def check_money():#查询余额
    with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f1:
        for line in f1:
            if line.split(':')[0]==user_state:
                res=line.strip().split(':')[3]
                print(res)

def withdraw_deposit():#提现
    is_withdraw_deposit=0
    with open(r'C:\Users\Administrator\Desktop\db.txt', mode='rt', encoding='utf-8') as f1:
        amount=input('请输入提款金额:')
        if not amount.isdigit():
            print('输入错误')
        else:
            for line in f1:
                if line.split(':')[0] == user_state:
                    money = line.strip().split(':')[3]
                    if int(res)-int(amount)>0:
                        left_money=int(res)-int(amount)
                        is_withdraw_deposit = 1
                        new_line=line.replace(f'{money}',f'{left_money}')
                        pass
                    else:
                        print('余额不足')
    if is_withdraw_deposit:
        list4=[]
        with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f2:
            for line in f2:
                if line.split(':')[0] == user_state:
                    line=new_line
                list4.append(line)
        with open (r'C:\Users\Administrator\Desktop\db.txt',mode='wt',encoding='utf-8') as f3:
            f3.write(list4)

def transfer():#转账
    is_transfer = 0
    with open(r'C:\Users\Administrator\Desktop\db.txt', mode='rt', encoding='utf-8') as f1:
        transfer_name=input('请输入要转入的账户:')
        amount=input('请输入转账金额:')
        if not amount.isdigit():
            print('输入错误')
        else:
            for line in f1:
                if line.split(':')[0]==transfer_name:
                    pass
            else:
                print('不支持对外账户哦 0.0')
            for line in f1:
                if line.split(':')[0] == user_state:
                    money = line.strip().split(':')[3]
                    if int(res)-int(amount)>0:
                        left_money=int(res)-int(amount)
                        is_transfer = 1
                        new_line=line.replace(f'{money}',f'{left_money}')
                        pass
                    else:
                        print('余额不足')
            for line in f1:
                if line.split(':')[0] == transfer_name:
                    money2 = line.strip().split(':')[3]
                    left_money2 = int(money2)+ int(amount)
                    new_line2=line.replace(f'{money2}',f'{left_money2}')
    if is_transfer:
        list5=[]
        with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f2:
            for line in f2:
                if line.split(':')[0] == user_state:
                    line=new_line
                elif line.split(':')[0] == transfer_name:
                    line=new_line2
                list5.append(line)

        with open (r'C:\Users\Administrator\Desktop\db.txt',mode='wt',encoding='utf-8') as f3:
            f3.write(list5)

おすすめ

転載: www.cnblogs.com/Franciszw/p/12550098.html