我很難創建一個以兩種不同方式移除「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來輸入我的函數時,輸入正常工作,但是當我退出時,它會給出一個錯誤 –
學習編程的一個重要部分就是了解錯誤消息的含義。 「給出錯誤」比實際的錯誤信息有用得多 –