1. 程式人生 > 其它 >測試學習筆記(P72-P76教程from凡雲教育)之習題python程式碼

測試學習筆記(P72-P76教程from凡雲教育)之習題python程式碼

P72

編寫註冊、登入程式碼

username = ['zhangsan', 'lisi', 'wangwu']
password = ['111111', '222222', '333333']

def reg():
    print('歡迎使用蝸牛ATM系統')
    un = input('請輸入註冊使用者名稱:')
    if un in username:
        print('使用者已註冊!')
        return False #結束reg函式
    else:
        pw = input('請輸入註冊密碼')
        if len(pw)<6:
            print('密碼長度小於6位,請檢查')
            return False
        else:
            username.append(un)
            password.append(pw)
            print('註冊成功!')
            return True

def login():
    un = input('請輸入使用者名稱:')
    pw = input('請輸入密碼:')
    if un in username:
        i = username.index(un)
        if pw == password[i]:
            print('登入成功!')
            return True
        else:
            print('使用者名稱或密碼錯誤!')
            return
    else:
        print('使用者名稱或密碼錯誤!')

if reg():
login()

P73

優化,新增迴圈

def reg():
    print('歡迎使用蝸牛ATM系統')
    while True:
        un = input('請輸入註冊使用者名稱:')
        if un in username:
            print('使用者已註冊!')
        else:
            break
    while True:
         pw = input('請輸入註冊密碼')
         if len(pw)<6:
             print('密碼長度小於6位,請檢查')
         else:
             break

    username.append(un)
    password.append(pw)
    print('註冊成功!')
    return True

def login():
    while True:
        un = input('請輸入登入使用者名稱:')
        pw = input('請輸入登入密碼:')
        if un in username:
            i = username.index(un)
            if pw == password[i]:
                print('登入成功!')
                return True
            else:
                print('使用者名稱或密碼錯誤!')
        else:
            print('使用者名稱或密碼錯誤!')


if reg():
login()

P74

用二位列表儲存並新增選單

users = [['zhangsan','111111'], [['lisi', '222222']], ['wangwu', '333333']]

def reg():
    print('歡迎使用蝸牛ATM系統')
    while True:
        un = input('請輸入註冊使用者名稱:')
        for item in users:
            if un == item[0]:
                print('使用者已註冊!')
                break
        else: # python 中存在一個for else 語句,如果for中能遍歷完,則接著執行else語句中的內容;如果for 未能便利完,則else 也不執行
         pw = input('請輸入註冊密碼')
         if len(pw)<6:
             print('密碼長度小於6位,請檢查')
         else:
            users.append([un, pw])
            print('註冊成功!')
            return True

def login():
    while True:
        un = input('請輸入登入使用者名稱:')
        pw = input('請輸入登入密碼:')
        for item in users:
            if un == item[0] and pw == item[1]:
                print('登入成功!')
                return True
        else:
            print('使用者名稱或密碼錯誤!')

def mymenu():
    menu = '''
    **************welcome to woniu ATM****************
    ***********please choose below potions************
    **********1. register 2. login 3. exit************
    '''
    while True:
        print(menu)
        option = input('請輸入您要操作的選單:')
        if option == '1':
            reg()
        elif option == '2':
            login()
        elif option == '3':
            print('感謝使用,歡飲下次再來!')
            break
        else:
            print('選單不存在,請重新輸入!')

mymenu()