生成指定长度的随机数字密码
生成指定长度的随机字母密码
生成指定长度的随机数字和字母的混合
#encoding=utf-8
import random
import string
class password_generator:
password_time=0
def __init__(self,length):
self.length=length
def digital_password(self):
password_generator.password_time+=1
s=""
for i in range(self.length):
s+=str(random.randint(0,9))
return s
@classmethod
def letter_password(cls,length):
password_generator.password_time+=1
s=""
for i in range(length):
s+=str( random.choice(string.letters))
return s
@staticmethod
def letter_mix_digital_password(length):
password_generator.password_time+=1
s=""
s1=""
nbr=random.randint(0,length)
for i in range(nbr):
s+=str( random.choice(string.letters))
for i in range(length-nbr):
s1+=str( random.randint(0,9))
return s+s1
a=password_generator(10)
print a.length
print a.digital_password()
print password_generator.letter_password(10)
print a.letter_mix_digital_password(10)
print "total password:",password_generator.password_time
c:Python27Scripts>python task_test.py
10
4775220675
xhqpSYbgGj
TjDIZwuB19
total password: 3