2012-09-27 68 views
1

我很难创建一个以两种不同方式移除“T”字母的函数。 parameterBoolean,如果参数是True那么它将打印出一个用户输入(用我的代码写在下面),它会一直询问相同的输入,直到用户输入“quit”并且用户键入“退出”,那么它将在字符串内部将所有小写字母“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:] 
+1

什么是你的代码怎么办呢?你有什么麻烦? – octern

+0

那么,当我输入true或false来输入我的函数时,输入正常工作,但是当我退出时,它会给出一个错误 –

+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 
+0

嘿,谢谢,但它给我一个语法错误,我目前正在使用python 2.4 –

+0

你会得到什么错误? (具体行) – arshajii

+0

@ A.R.S。最后一行 - 条件表达式直到2.5时才引入 –

相关问题