1. 程式人生 > >登錄接口,猜年齡

登錄接口,猜年齡

please userinfo true main 程序 range muc for 說明

編寫登陸接口
基礎需求:
1. 讓用戶輸入用戶名密碼
2. 認證成功後顯示歡迎信息
3. 輸錯三次後退出程序
升級需求:
4. 可以支持多個用戶登錄 (提示,通過列表存多個賬戶信息)
5. 用戶3次認證失敗後,退出程序,再次啟動程序嘗試登錄時,還是鎖定狀態(提示:需把用戶鎖定的狀態存到文件裏)

#!/usr/bin/env python
# -*- coding:utf-8 -*-  
# by wk

‘‘‘
說明:
如果登錄用戶在用戶列表裏,每個用戶只有3次登錄機會,失敗後鎖定賬戶,下次啟動賬戶提示賬戶被鎖定
如果登錄用戶不在用戶列表裏,提示用戶不存在,並且嘗試3次登錄,如果失敗退出程序
‘‘‘ import sys def checklock(username): with open("loginnum.txt", r) as lock_t: for line in lock_t.readlines(): if len(line) == 0: continue if username == line.strip(): print("login fail too much! User locked") # sys.exit()
return lock def userlock(username): f = open("loginnum.txt", a) f.write(username + \n) f.close() def userlogin(userinfo): count = 3 #計數器 flag = success #標記登錄狀態 userid = 0 #標記用戶編號 while True:
# if userinfo[userid][‘login_fail‘] != 0: username = input(please enter your name: ) password = input(please enter your password: ) if checklock(username) == lock: continue for i,user in enumerate(userinfo): if user[username] == username and user[password] == password: flag = success break elif user[username] == username and user[password] != password: flag = fail userid = int(i) break else: flag = nouser if flag == success: print(login successful, welcome!) break elif flag == fail: # count -= 1 print(who , userinfo[userid]) failnum = int(userinfo[userid][login_fail]) #統計登錄失敗次數 failnum -= 1 if userinfo[userid][login_fail] == 1: print(your account lock) userlock(userinfo[userid][username]) sys.exit() else: userinfo[userid][login_fail] = failnum print(login fail, you have %s choice % (failnum)) else: count -= 1 if count == 0: print(you login too much time) sys.exit() print(Sorry no user, you have %s choice % (count)) # else: # break if __name__ == __main__: userinfo = [ {username:eric, password:123456, login_fail:3}, {username:tom, password:123456, login_fail:3}, {username:jerry, password:123456, login_fail:3}, ] # password = [‘123456‘,‘123456‘,‘123456‘] userlogin(userinfo)


猜年齡遊戲升級版
要求:
1. 允許用戶最多嘗試3次
2. 每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往復,如果回答N或n,就退出程序
3. 如何猜對了,就直接退出

#!/usr/bin/env python
# -*- coding:utf-8 -*-  
# by wk

‘‘‘
說明:
1. 允許用戶最多嘗試3次
2. 每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往復,如果回答N或n,就退出程序
3. 如何猜對了,就直接退出

‘‘‘

def guessage(my_age):
    flag = ‘‘
    while True:
        count = 3
        for i in range(3):                  #循環3次
            try:
                age = int(input(please enter age: ))
                if age > my_age:
                    count -= 1              #統計輸錯次數
                    print(you guess too big, you have %s chance %count)
                    if i == 2:              #猜錯3次後詢問還繼續猜不
                        # print(‘you have no chioce‘)
                        flag = input(Do you want try again Y/N ? )
                elif age < my_age:
                    count -= 1              #統計輸錯次數
                    print(you guess too smaill, you have %s chance %count)
                    if i == 2:              #猜錯3次後詢問還繼續猜不
                        # print(‘you have no chioce‘)
                        flag = input(Do you want try again Y/N ? )
                else:
                    print(you guess it !)      #猜對了退出
                    flag = N
                    break;
            except:                         #如果輸入的不是數字,提示請輸入數字
                count -= 1
                print(please enter number, you have %s chance %count)
        if flag == Y or flag == y:
            continue
        elif flag == N or flag == n:
            break
        else:
            print(Wrong choice !!!)       #如果輸入的不是Y,y,N,n,直接退出
            break

if __name__ == __main__:
    my_age = 32
    guessage(my_age)

登錄接口,猜年齡