首页 > 编程语言 >生成你的自定义密码本Python

生成你的自定义密码本Python

时间:2023-03-07 18:44:35浏览次数:43  
标签:自定义 combination Python length 密码本 input words name

python生成一个自定义密码本

import itertools as its
import os

# 定义生成密码本的函数

def generate_passwords(length, combination):
    if combination == "1":
        words = "1234567890"
    elif combination == "2":
        words = "abcdefghijklmnopqrstuvwxyz"
    elif combination == "3":
        words = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    elif combination == "4":
        words = "!@#$%^&*()_+"
    else:
        words = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+"

    r = its.product(words, repeat=length)
    file_name = f"{length}_{combination}.txt"
    if os.path.isfile(file_name):
        print(f"{file_name} already exists. Please choose a different length or combination.")
    else:
        with open(file_name, "a") as f:
            for i in r:
                f.write("".join(i) + "\n")
        print(f"{file_name} generated successfully!")


# 主函数,获取用户输入并生成密码本

def main():
    length = input("输入生成密码的长度: ")
    while not length.isdigit():
        length = input("Invalid input. 重新输入: ")

    combination = input("Please choose the combination of the password:\n1. 数字\n2. 小写字母\n3. 大写字母\n4. 特殊字符\n5. 所有组合\n")
    while not set(combination).issubset(set("12345")) or not combination:
        combination = input("Invalid input. 重新输入: ")
    
    generate_passwords(int(length), combination)

if __name__ == "__main__":
    main()

运行结果如下:

请输入您要生成的密码长度: 10
请选择密码组合方式:
1.纯数字 2.字母小写 3.字母大写 4.特殊字符 5.所有组合
请输入对应数字:1234
生成的密码为:  C4D.)({+/# 

用户可以通过输入不同的数字来选择密码组合方式,也可以通过输入数字串来选择多种组合方式的结合。

生成一个随机密码和一个当前组合方式的密码本

标签:自定义,combination,Python,length,密码本,input,words,name
From: https://www.cnblogs.com/Athenavi/p/17189136.html

相关文章

  • Python第三方库request的安装及基本用法
    ​1、安装安装命令:pipinstallrequests豆瓣源安装:pipinstallrequests-ihttps://pypi.douban.com/simple/​2、requests常见参数​url参数:传入的是字符串,请求地址data......
  • python-函数
    函数的定义先定义再使用def定义函数的时候代码不执行调用函数的时候代码才执行函数的参数形参定义函数时候的参数形参必须是......
  • 22个受欢迎的Python不同类型开源框架
    22个受欢迎的Python不同类型开源框架记录:一、PythonWeb框架Django:PythonWeb应用开发框架链接:https://www.djangoproject.com/Django应该是最出名的Python框架,GAE甚......
  • python小练习——图书管理系统(增加数据存储)
     上一次我们做了一个小的图书馆里系统,用来学习python基础部分的:函数、模块、列表、字典、循环、判断 现在我们在上一次的基础上增加一个功能,将写入系统的书籍存放起来......
  • Python3,2分钟掌握Doscoart库,你也能成为艺术家。
    1、引言小屌丝:鱼哥,最近在忙啥?小鱼:咱俩陌生了?小屌丝:何出此言?小鱼:你说的话又嘛意思呢?小屌丝:我的意思,最近看你这整理各种资料,貌似很忙的样子?小鱼:我平时不也这么忙嘛小......
  • python操作pandas的笔记
    importpandasaspddata={'name':['Alice','Bob','Charlie','David'],'age':[25,30,35,40],'gender':['F','M','M','M'......
  • Python中Index的用法
    1.Index常用于Python的List数据类型在Python中有一种数据类型叫作List数据类型。程序员口中和中文翻译过来都称之为List数据类型,而Index主要用于List数据类型中。Index......
  • 直播平台源代码,Android自定义View实现呼吸灯效果
    直播平台源代码,Android自定义View实现呼吸灯效果自定义View自定义BreathView的Kotlin代码如下: importandroid.animation.ValueAnimatorimportandroid.animation.V......
  • 使用Python操作Mysql数据库(进阶)
    #-*-coding:utf-8-*-importloggingimportpymysqlfromrest_framework.responseimportResponselogger=logging.getLogger(__name__)#连接数据库def......
  • Python 内置函数装饰器 classmethod staticmethod
    使用官方的说法:classmethod(function)中文说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:classC:@classmetho......