欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python装饰器模拟登录系统

程序员文章站 2022-07-15 08:17:35
...
#装饰器:
import time
import random

islogin = False
dict1 = {'08172357':'123456','0':'0'}

def random_number(n):
	list1 = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
	code = ''
	for i in range(n):
		code += random.choice(list1)
	return code

	
def write_down():
	print('您正在注册——————————')
	time.sleep(0.5)
	while True:
		user_Name = input('请输入用户名:')
		user_Password = input('请输入密码:')
		user_Password_Again = input('请确认密码:')
		#a = random_number(random.randint(4,7))
		b = random.randint(4,7)
		a = random_number(b)
		print('验证码为:%s'%a)
		yan = input('请输入验证码:')
		if user_Password != user_Password_Again:
			print('密码错误,请重新注册!')
			continue
		if yan != a :
			print('验证码错误,请重新注册!')
			continue
		dict1[user_Name] = user_Password
		print('注册成功,即将跳转登录系统..........')
		break


def login():
	print('您正在登入系统————————————')
	time.sleep(0.5)
	global islogin
	while True:
		user_Name = input('请输入用户名:')
		user_Password = input('请输入密码:')
		#if dict1.get(user_Name) == user_Password:
		if dict1.get(user_Name,1) == user_Password:
			print('登录成功!')
			islogin = True
			break
		else :
			print('密码错误,请重新登录!')
			islogin = False
			time.sleep(1)


def login_Required(func):
	def wrapper(*args,**kargs):
		#验证用户是否登录
		while True:

			if islogin :
				func(*args,**kargs)
				break
			else :
				print('需登录才可付款:','  已经拥有账号请输入Y','  需要注册请输入N',sep = '\n')
				s = input('Y or N:')
				s = s.lower()
				if s == 'y' :
					login()
					continue
				else :
					write_down()
					time.sleep(1.5)
					login()
	return wrapper

@login_Required
def pay():
	money = input('您要付款多少元?:')
	print('正在付款,金额为{}'.format(money))
	print('正在付款......')
	time.sleep(1.5)
	print('付款成功!')
pay()