1

我很难创建一个以两种不同方式删除“T”字母的函数。是parametera Boolean,如果参数是True那么它将打印出一个用户输入(写在我下面的代码中)并且它会一直询问相同的输入,直到该人输入“退出”并且当用户输入“退出”时然后它将删除字符串内的所有小写“t”。如果BooleanFalse,则应用相同的过程,但不是取出小写“t”,而是取出大写“T”。另外,我想保持我在下面的代码中编写的相同类型的格式,谢谢。顺便说一句,我使用的是 python 2.4。

def removeT(Boolea):
    userInput=str(input("Enter a word/sentence you want to process: "))
    while userInput:
        string = (raw_input("Enter a word/sentence you want to process:"))
        if Boolea == False:
            userInput = userInput + string
            while "T" in userInput:
                index = userInput.find("T")
                userInput = userInput[:index] + userInput[index+1:]
        if string == "quit":
            keepAsking = False
            return userInput

        else:
            userInput = userInput + string
            while "t" in userInput:
                index = userInput.find("t")
                userInput = userInput[:index] + userInput[index+1:]
            while "T" in userInput:
                index = userInput.find("T")
                userInput = userInput[:index] + userInput[index+1:]
4

1 回答 1

0
def removeT(b):
    s1 = ""
    while True:
        s2 = raw_input("Enter a word/sentence you want to process: ")
        if s2 == "quit": return s1
        s2 = s2.replace("T", "")
        if b:
            s2 = s2.replace("t", "")
        s1 += s2
于 2012-09-27T02:54:03.707 回答