使用for循环Python

时间:2018-11-23 18:52:30

标签: python

我只需要使用if和while语句,就需要删除所有多余的空格,并在单词之间留一个空格。然后说明已删除的字符数量和新句子 编辑,它还必须适用于句子中包含的标点符号。 这是我想出的,但是它只剩下我选择的句子的第一个字母作为数字和最后一个句子。谁能帮忙。

def cleanupstring(S):

    lasti = ""
    result = ""
    for i in S:
        if lasti == " " and i == " ":
            i = ""
        else:
            lasti = i    
        result += i    

    return result

sentence = input("Enter a string: ")

outputList = cleanupstring(sentence)

print("A total of", outputList[1], "characters have been removed from your string.")
print("The new string is:", outputList[0]) 

1 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

 Generating... 

               done in 0.095 seconds.

计数器会记录您删除字符的频率,[0]和[1]现在以您希望的方式工作。
这是因为outputList现在是一个元组,索引0的第一个值现在是结果,索引1的第二个值是计数器。

相关问题