我很难创建一个以两种不同方式移除“T”字母的函数。 parameter
是Boolean
,如果参数是True
那么它将打印出一个用户输入(用我的代码写在下面),它会一直询问相同的输入,直到用户输入“quit”并且用户键入“退出”,那么它将在字符串内部将所有小写字母“t”全部取出。如果Boolean
是False
,那么同样的过程适用,但不是取出小写字母“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:]
什么是你的代码怎么办呢?你有什么麻烦? – octern
那么,当我输入true或false来输入我的函数时,输入正常工作,但是当我退出时,它会给出一个错误 –
学习编程的一个重要部分就是了解错误消息的含义。 “给出错误”比实际的错误信息有用得多 –