首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何简化这个首字母缩写生成器?

如何简化这个首字母缩写生成器?
EN

Stack Overflow用户
提问于 2021-03-03 07:28:28
回答 1查看 69关注 0票数 0

我写了首字母缩写生成器的代码。我只能让它工作十个字。

我觉得代码是不必要的重复。有更简单的方法吗?它能对一组更大的词起作用吗?

我刚开始编码,我对编程一无所知,所以我可能需要在你的答案中详细解释。

代码语言:javascript
复制
userInput = input('enter name of the organization not more than ten words: ')
listInput = userInput.split()


if len(listInput) == 1:
    a = listInput[0]
    acronym = a[0]
    print(acronym.upper())
elif len(listInput) == 2:
    a = listInput[0]
    b = listInput[1]
    acronym = a[0] + b[0]
    print(acronym.upper())
elif len(listInput) == 3:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    acronym = a[0] + b[0] + c[0]
    print(acronym.upper())
elif len(listInput) == 4:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    acronym = a[0] + b[0] + c[0] + d[0]
    print(acronym.upper())
elif len(listInput) == 5:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    e = listInput[4]
    acronym = a[0] + b[0] + c[0] + d[0] + e[0]
    print(acronym.upper())
elif len(listInput) == 6:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    e = listInput[4]
    f = listInput[5]
    acronym = a[0] + b[0] + c[0] + d[0] + e[0] + f[0]
    print(acronym.upper())
elif len(listInput) == 7:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    e = listInput[4]
    f = listInput[5]
    g = listInput[6]
    acronym = a[0] + b[0] + c[0] + d[0] + e[0] + f[0] + g[0]
    print(acronym.upper())
elif len(listInput) == 8:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    e = listInput[4]
    f = listInput[5]
    g = listInput[6]
    h = listInput[7]
    acronym = a[0] + b[0] + c[0] + d[0] + e[0] + f[0] + g[0] + h[0]
    print(acronym.upper())
elif len(listInput) == 9:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    e = listInput[4]
    f = listInput[5]
    g = listInput[6]
    h = listInput[7]
    i = listInput[8]
    acronym = a[0] + b[0] + c[0] + d[0] + e[0] + f[0] + g[0] + h[0] + i[0]
    print(acronym.upper())
else:
    a = listInput[0]
    b = listInput[1]
    c = listInput[2]
    d = listInput[3]
    e = listInput[4]
    f = listInput[5]
    g = listInput[6]
    h = listInput[7]
    i = listInput[8]
    j = listInput[9]
    acronym = a[0] + b[0] + c[0] + d[0] + e[0] + f[0] + g[0] + h[0] + i[0] + j[0]
    print(acronym.upper())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-03 07:31:01

使用循环、生成器表达式理解来处理动态长度数据:

代码语言:javascript
复制
userInput = input('enter name of the organization not more than ten words: ')
listInput = userInput.split()

acro = ''.join(token[0].upper() for token in listInput)
print(acro)

这使用带有生成器表达式的str.join将首字母组合成单个字符串。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66452490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档