-1
對於介紹comp sci實驗我試圖構建一個遞歸函數,它計算字符串中給定字母的出現次數,而不使用內置方法或循環。我非常難以使用myStr.count。任何人都可以提供小費嗎?我應該嘗試一些檢查myStr [0]的身份並返回0如果信!= myStr [0]?在Python 3中構建遞歸函數
def count(letter, myStr, theLen):
#Code for def count
def main():
print ("The count of letter a is: ", count("a", "abacdadaad", 10))
print ("The count of letter b is: ", count("b", "abacdadaad", 10))
print ("The count of letter c is: ", count("c", "abacdadaad", 10))
print ("The count of letter d is: ", count("d", "abacdadaad", 10))
main()
請妥善IDENT你的代碼,與Python這是一個非常了不起:) – jozefg
*我應該嘗試的東西,檢查myStr的[0]的身份,如果信返回0! = myStr [0] * - 你絕對是在正確的軌道上。同樣想想letter == myStr [0]會發生什麼,然後遞歸地實現。 –
另外,請使用適當的PEP8風格。即沒有camelCase(也是'myStr'和'theLen',不管是哪種風格都是可怕的名字),函數名和左括號之間沒有空格。 – ThiefMaster