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

如何使用Python实现基于输入姓名字母的随机形容词生成器

Python 姓名逐字母随机形容词生成器实现

原有代码问题

  • 遍历姓名字符串时,循环变量i取到的是单个字符,判断条件i == "Peter"永远无法触发
  • 未做字母大小写兼容,大小写混合输入时容易匹配失败
  • 未预置各字母对应的形容词词库,也没有随机选词逻辑

实现思路

  • 先搭建A-Z所有字母对应的形容词词库,每个字母对应多个可选形容词
  • 接收用户输入后统一做大小写转换,跳过非字母字符避免运行报错
  • 遍历每个有效字母,从对应词库中随机抽取形容词,按要求格式输出

可直接运行的完整代码

import random

# 各字母对应形容词词库,可自行增删修改
ADJECTIVE_LIB = {
    "A": ["Adorable", "Adventurous", "Amazing", "Artistic", "Awesome"],
    "B": ["Brave", "Bright", "Brilliant", "Bold", "Balanced"],
    "C": ["Calm", "Clever", "Creative", "Cheerful", "Confident"],
    "D": ["Daring", "Dependable", "Dynamic", "Delightful", "Determined"],
    "E": ["Elegant", "Energetic", "Excellent", "Enthusiastic", "Extraordinary"],
    "F": ["Friendly", "Funny", "Fantastic", "Fearless", "Frank"],
    "G": ["Generous", "Gentle", "Gifted", "Glorious", "Good-natured"],
    "H": ["Happy", "Hardworking", "Helpful", "Honest", "Humorous"],
    "I": ["Imaginative", "Independent", "Intelligent", "Innovative", "Inspiring"],
    "J": ["Joyful", "Judicious", "Jovial", "Just", "Jazzy"],
    "K": ["Kind", "Knowledgeable", "Keen", "Kooky", "Knightly"],
    "L": ["Lively", "Lovable", "Loyal", "Logical", "Laid-back"],
    "M": ["Magnificent", "Modest", "Motivated", "Meticulous", "Merry"],
    "N": ["Nice", "Noble", "Nurturing", "Neat", "Noteworthy"],
    "O": ["Optimistic", "Open-minded", "Outstanding", "Original", "Observant"],
    "P": ["Perfect", "Polite", "Powerful", "Practical", "Pretty"],
    "Q": ["Quick-witted", "Quiet", "Qualified", "Quirky", "Quality"],
    "R": ["Reliable", "Resourceful", "Romantic", "Respectful", "Resilient"],
    "S": ["Sincere", "Smart", "Strong", "Sociable", "Splendid"],
    "T": ["Tall", "Thoughtful", "Trustworthy", "Talented", "Tenacious"],
    "U": ["Understanding", "Unique", "Unstoppable", "Upbeat", "Useful"],
    "V": ["Vibrant", "Victorious", "Vigilant", "Vivacious", "Valiant"],
    "W": ["Warm", "Wise", "Witty", "Wonderful", "Welcoming"],
    "X": ["Xenial", "X-factor", "Xtraordinary", "Xenodochial", "Xciting"],
    "Y": ["Youthful", "Yummy", "Yare", "Yielding", "Yonder"],
    "Z": ["Zealous", "Zany", "Zestful", "Zen", "Zippy"]
}

if __name__ == "__main__":
    name = input("Enter your name here: ").strip()
    for char in name:
        upper_char = char.upper()
        # 跳过空格、符号等非字母字符
        if upper_char not in ADJECTIVE_LIB:
            continue
        # 随机选词、随机选连接词
        selected_adj = random.choice(ADJECTIVE_LIB[upper_char])
        connector = random.choice(["for", "as in"])
        print(f'"{upper_char}" {connector} {selected_adj}')

运行效果示例(输入PETER时)

"P" for Perfect
"E" as in Elegant
"T" for Tall
"E" for Energetic
"R" as in Reliable

自定义调整说明

  • 可直接修改ADJECTIVE_LIB里的形容词列表,加入更多你想要的词汇
  • 如果不需要随机切换for/as in连接词,直接把connector变量改成固定值即可
  • 如果需要保留输入字母的原始大小写,把print语句里的upper_char替换成原始遍历的char即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 11:45:34