2014-07-12 97 views
3

當我嘗試在我的程序來使用這個,它說,有一個屬性錯誤AttributeError的:「builtin_function_or_method」對象有沒有屬性「取代」

'builtin_function_or_method' object has no attribute 'replace' 

,但我不明白爲什麼。

def verify_anagrams(first, second): 
    first=first.lower 
    second=second.lower 
    first=first.replace(' ','') 
    second=second.replace(' ','') 
    a='abcdefghijklmnopqrstuvwxyz' 
    b=len(first) 
    e=0 
    for i in a: 
     c=first.count(i) 
     d=second.count(i) 
     if c==d: 
      e+=1 
    return b==e 

回答

6

您需要呼叫str.lower法通過後,將()

first=first.lower() 
second=second.lower() 

否則,firstsecond將被分配給函數對象本身:

>>> first = "ABCDE" 
>>> first = first.lower 
>>> first 
<built-in method lower of str object at 0x01C765A0> 
>>> 
>>> first = "ABCDE" 
>>> first = first.lower() 
>>> first 
'abcde' 
>>> 
+0

謝謝你這麼多,我沒有看到正確的地方! – 1089

相關問題