2012-09-27 70 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時才引入 –

相關問題