我写了首字母缩写生成器的代码。我只能让它工作十个字。
我觉得代码是不必要的重复。有更简单的方法吗?它能对一组更大的词起作用吗?
我刚开始编码,我对编程一无所知,所以我可能需要在你的答案中详细解释。
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())https://stackoverflow.com/questions/66452490
复制相似问题