0
printmystring = ""

mystring = input("Enter the sentence you wish to be an acronym: ")

mystring_length = len(mystring)

for i in range (0,mystring_length):
    if  mystring[i].isupper():
        print(mystring[i] ,end="")
        print(".",end="")

我的输入是“供您参考”

输出仅供参考

4

4 回答 4

2

对于这么小的任务,代码似乎相当复杂。

让我提出一个不同的解决方案,我们将所有大写字母收集在一个列表中。

mystring = input("Enter the sentence you wish to be an acronym: ")
upper_letters = [letter for letter in mystring if letter.isupper()]
print('.'.join(upper_letters))

moarningsun 提出了另一种方法:

upper_letters = filter(str.isupper, mystring)
于 2013-10-28T18:17:17.290 回答
0

我很确定这会起作用:

mystring = input("Enter the sentence you wish to be an acronym: ")

mystring_length = len(mystring)

print(mystring[0] ,end="")

for i in range (1,mystring_length):
    if mystring[i].isupper():
        print(". " ,end="") 
        print(mystring[i],end="")
于 2013-10-28T18:08:32.900 回答
0

你在寻找这样的东西吗:

#initialize variables
buffer = ""
words = list()

#get input
mystring = input("Enter the sentence you wish to be an acronym: ")
mystring_length = len(mystring)

#parse words
for i in range (0, mystring_length)
     if mystring[i] == " "
         words.append(buffer)
         buffer = ""
     else
         buffer += mystring[i]

#add the final word to buffer
#as it is not followed by a space, it will not be added using the above algorithm
words.append(buffer)
buffer = ""

#loop through words
for i in range (0, len(words))
     print(words[i], end="")
     if (i != len(words))
          print(".", end="")

为python编辑(我认为)

于 2013-10-28T18:08:50.717 回答
0
def acronym(sentence):
    return '.'.join([x[0].upper() for x in sentence.split()])    
于 2013-10-28T18:54:09.357 回答