标签:use ping utf-8 word ati auth 支持 添加 erro
作业需求:
模拟实现一个ATM + 购物商城程序
1、额度15000或自定义功能。
# Author: ray.zhang import sys while True: user = str(input("\033[1;32;40mPlease input your name:\033[0m")) f = open(‘user_list‘, ‘r+‘) for line in f.readlines(): n = str(line.split()[0]) p = str(line.split()[1]) if user == n: while True: password = str(input("\033[1;32;40mPlease input your password:\033[0m")) if password != p: print "\033[1;31;40mThe password is incorrect\033[0m" continue else: print "\033[1;32;40myes let you in.\033[0m" global money money_total = 15000 print("\033[1;33;40mYou total money is: \033[0m", money_total) exit() else: print "\033[1;31;40mThe user is not vaild, please re-input:\033[0m" continue
2、支持多账户登录
# Author: ray.zhang dict = { ‘user1‘:{‘pass‘:123,‘count‘:0}, ‘user2‘:{‘pass‘:456,‘count‘:0}, ‘user3‘:{‘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
3、实现用户认证装饰器
# Author: ray.zhang import time user_dict={‘user‘:None} def auth(func): def wrapper(): print(user_dict[‘user‘]) #user = user_dict[‘user‘] #print(user_dict[‘user‘]) if user_dict[‘user‘]: starttime = time.time() func() stoptime = time.time() print("Run time is %s" % (stoptime - starttime)) if not user_dict[‘user‘]: user = input("plz input your user:").strip() passwd = int(input("plz input your passwd:").strip()) with open(‘info‘,‘r‘,encoding=‘utf-8‘) as f: date=eval(f.read()) starttime = time.time() if user in date and passwd == date[user]: # if user == "erav" and passwd == 123: print("you input success...") func() stoptime = time.time() print("Run time is %s" %(stoptime - starttime)) user_dict[‘user‘] = user else: print("you input error...") return wrapper @auth #这个就相当于 timmer=auth(timmer) def timmer(): time.sleep(2) print("welcome to shopping") timmer()
标签:use ping utf-8 word ati auth 支持 添加 erro
原文地址:http://www.cnblogs.com/zhangray/p/7248302.html