1、可以支持多个用户登录 (提示,通过字典存多个账户信息)。用户3次认证失败后,退出程序,
#!/usr/bin/env python3 dict = { 'jack':{'pass':123,'count':0}, 'eray':{'pass':456,'count':0}, 'bike':{'pass':789,'count':0} } name = input ("plz input your name: ") print (dict[name]['pass']) if name not in dict: print ("Sorry,you input error.") exit() elif name in dict : print ("Ok,you input success.") while dict[name]['count'] < 3: pas = int(input("plz input your passwd: ")) if pas == dict[name]['pass']: print ("Congratulation to you.") dict[name]['count'] = 3 else: dict[name]['count'] += 1
2、可以支持多个用户登录 (提示,通过列表存多个账户信息)
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
#!/usr/bin/env python3 #-*- coding:utf-8 -*- #Author: Zhang Lei userdict = {"enven":111,"jack":222,"eray":333,"bike":444} count = 0 num = 0 while count < 3: username = input("What's your name? ") f = open("name.txt") date = f.readlines() date = ''.join(date).strip('\n') f.close() if username in date: print ("sorry,your account is blocked.") break else: if username not in userdict: print("no,you guess error...") count += 1 continue elif username in userdict: print("year,input success...") while num < 3: passwd = input("plz input your passwd:") if passwd == str(userdict[username]): print("Yes, congratulations to you...") break elif num == 2: print("You are locked:", username) f = open('name.txt','a+') f.write(username +'\n') f.close break else: print("plz input your passwd again:") num += 1 break
3、允许用户尝试3次, 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答是Y 或y,则继续让其猜3次,一次往复,如果回答N或n,就退出程序,
如果猜对了,就直接退出。
#!/usr/bin/env python3 # -*- coding:utf-8 -*- #Author: Zhang Lei count = 0 user = "eray" while count < 3: name = input("Plz guess name: ") if user == name: print("Contratulations. You got it.") break elif count == 2: guess = input("Do you want to guess again?\nplz input y/n\n") if guess == "Y" or guess == "y": print ("Let's go run...") count = 0 continue elif guess == "N" or guess =='n': print("Sorry,Your guess is stoped.") break else: print("you input error.") break else: print ("Sorry,You guess error.") count += 1
上述例子采用标签的方式,如下:
#!/usr/bin/env python3 # -*- coding:utf-8 -*- #Author: Zhang Lei count = 0 user = "eray" tag = True while tag: name = input("Plz guess name: ") if user == name: print("Contratulations. You got it.") tag = False elif count == 2: guess = input("Do you want to guess again?\nplz input y/n\n") while tag: if guess == "Y" or guess == "y": print ("Let's go run...") count = 0 break elif guess == "N" or guess =='n': print("Sorry,Your guess is stoped.") tag = False else: print("you input error.") tag = False else: print ("Sorry,You guess error.") count += 1