You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将随机密码生成器改为使用string模块的ASCII字符串

如何用string模块替换密码生成器中的手动字符组?

直接将原代码里手动编写的字符集合替换为string模块的内置ASCII常量即可,对应关系如下:

  • 数字字符串 "0123456789" → string.digits
  • 大写字母字符串 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" → string.ascii_uppercase
  • 小写字母字符串 "abcdefghijklmnopqrstuvwxyz" → string.ascii_lowercase
  • 特殊符号字符串 "!#$%&'()*+,-./:;<=>?@[\\]^_{|}~"` → `string.punctuation`

修改后的完整代码:

import random
import string

def UltimatePasswordGenerator():
    print('====================================================')
    print('==== WELCOME TO THE ULTIMATE PASSWORD GENERATOR ====')
    print('====================================================')
    try:
        length = int(input("Please enter the length of the password you want to create:\nNote: The minimum length is 8 characters And enter Only Numbers\n"))
        while length < 8:
            print("The minimum password length is 8 characters, please enter a valid number ")
            length = int(input("Enter the length of the password you want to create:\n"))
        password_uppercase = input("Answer only with yes or no.\nDo you want upper case? \n")
        while password_uppercase != "yes" and password_uppercase != "YES" and password_uppercase != "no" and password_uppercase != "NO":
            password_uppercase = input("Answer only with yes or no.\nDo you want upper case? \n")
        password_lowercase = input("Answer only with yes or no.\nDo you want lower case?\n")
        while password_lowercase != "yes" and password_lowercase != "YES" and password_lowercase != "no" and password_lowercase != "NO":
            password_lowercase = input("Answer only with yes or no.\nDo you want lower case?\n")
        password_numbers = input("Answer only with yes or no.\nDo you want numbers?\n")
        while password_numbers != "yes" and password_numbers != "YES" and password_numbers != "no" and password_numbers != "NO":
            password_numbers = input("Answer only with yes or no.\nDo you want numbers?\n")
        password_special = input("Answer only with yes or no.\nDo you want Special symbols?\n")
        while password_special != "yes" and password_special != "YES" and password_special != "no" and password_special != "NO":
            password_special = input("Answer only with yes or no.\nDo you want Special symbols?\n")
    except ValueError:
        print("Enter Only Number above 8")
    except:
        print("Sorry you got an error! Please enter a VALID value")
    else:
        alphabets = []   # keep a list of alphabet groups 
        if password_numbers in ("yes", "YES"):
            alphabets.append(string.digits)  # 替换为string模块的数字常量
        if password_uppercase in ("yes", "YES"):
            alphabets.append(string.ascii_uppercase)  # 替换为string模块的大写字母常量
        if password_lowercase in ("yes", "YES"):
            alphabets.append(string.ascii_lowercase)  # 替换为string模块的小写字母常量
        if password_special in ("yes", "YES"):
            alphabets.append(string.punctuation)  # 替换为string模块的特殊符号常量
        password_chars = []  # construct the password as a list
        while len(password_chars) < length:  # keep choosing until length requirement is met
            for alphabet in alphabets:  # for each group
                password_chars.append(random.choice(alphabet)) # choose a character from the group
                if len(password_chars) > length:
                    break
        random.shuffle(password_chars) # shuffle to lose the order of groups from the generated list
        return "".join(password_chars)

print(UltimatePasswordGenerator())

这样修改的好处是:无需手动编写容易出错的字符集合,string模块的内置常量已经封装好了标准的ASCII字符组,代码更简洁、规范且不易出错。

内容的提问来源于stack exchange,提问作者Tydus X

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:05:37