2017-02-26 22 views
-4

DEF countLetters(計數):我不知道這個Python代碼有什麼問題,但它說「預計一個縮進塊」?

letterList = []

在計數字母: 如果信不在letterList: letterList.append(字母)

返回letterList

詞語= open(「/ Users/Sammi/Desktop/Hw3_nanisama/words.txt」,「r」)

print(words)

打印( 「文件長度:」,LEN(字))

打印( 「一」,LEN(aCount))

打印( 「B:」,LEN(bCount))

的emptyList =名單()

+1

讀一本Python書... –

+2

[Python期望的縮進塊錯誤]的可能重複(http://stackoverflow.com/questions/12632615/python-expected-an-indented-block-error) – miradulo

+0

Python是[偏移規則](https: //en.wikipedia.org/wiki/Off-side_rule)語言,從而定義帶有縮進的代碼塊。這段代碼沒有正確縮進。 –

回答

-2

我認爲你需要學習Python語法, 基本你必須寫爲下面的代碼:

def countLetters(count): 

    letterList = [] 

    for letter in count: 
     if letter not in letterList: 
      letterList.append(letter) 

    return letterList 

words = open("/Users/Sammi/Desktop/Hw3_nanisama/words.txt", "r") 

print(words) 

print("File length: ", len(words)) 

print("a: ", len(aCount)) 

print("b: ", len(bCount)) 

emptyList = list() 
相關問題