分享两种生成随机用户名和密码的方法
这篇文章主要介绍了python编程生成随机用户名及密码的方法,结合实例形式分析了python随机字符串的相关操作技巧,需要的朋友可以参考下
本文实例讲述了Python编程生成随机用户名及密码的方法。分享给大家供大家参考,具体如下:
方案一:
import randomglobal userName,userPassword #为了便于使用,定义为全局变量userName = ''userPassword = ''def get_userNameAndPassword(): global userName, userPassword usableName_char = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><: try: except><p>程序输出:</p><pre class="brush:py;">e_userName = ['q', 'M', '2', 'R', 'B', '}', '6', '=']e_userPassword = ['T', 'O', '4', 'C', 'H', '.']用户名: qM2RB}6=密码: TO4CH.
方案二(省去中间变量):
#coding=utf-8import randomglobal userName,userPassword #为了便于后面使用,定义为全局变量userName = ''userPassword = ''def get_userNameAndPassword(): global userName, userPassword #8位用户名及6位密码 userName = ''.join(random.sample("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><: try: except><p>程序输出:</p><pre class="brush:py;">用户名: GweV?2um密码: fwiOZL
常用第二种方法,直观简便。
【