登录接口,猜年龄

编写登陆接口
基础需求:
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)

 

posted @ 2017-07-06 11:33  GodSpeed034  阅读(252)  评论(0编辑  收藏  举报