import random
def verifycode():
code_list = ""
# 每一位验证码都有三种可能:大写字母、小写字母、数字
for i in range(6): # 控制验证码生成的位数
state = random.randint(1, 3)
if state == 1:
first_kind = random.randint(65, 90) # 大写字母
random_uppercase = chr(first_kind)
code_list = code_list + random_uppercase
elif state == 2:
second_kinds = random.randint(97, 122) # 小写字母
random_lowercase = chr(second_kinds)
code_list = code_list + random_lowercase
elif state == 3:
third_kinds = random.randint(0, 9)
code_list = code_list + str(third_kinds)
return code_list
if __name__ == "__main__":
verifycode = verifycode()
print(verifycode)
"""
程序如何判断一个.py文件是作为脚本还是模块?
关键是一个名为__name__的变量
如果它的值是__main__ ,则不能作为模块,只能作为脚本直接运行
如果在其他程序中被当做模块调用了,那么if 语句之下的内容不会被执行
"""
随机验证码生成
最新推荐文章于 2021-06-18 20:19:23 发布