2015-04-04 59 views
-3

我是新來的python,並且遇到字符串問題。我得到「AttributeError:'str'對象沒有任何屬性」,但爲什麼會感到困惑。我提供了一些我的代碼,所以任何建議都會有所幫助!AttributeError:'str'對象沒有屬性isalpha()

#Have user enter their string. 
string = input("Enter a string: ") 
    #Find if string only contains letters and spaces 
    if string.isalpha(): 
     print("Only alphabetic letters and spaces: yes") 
    else: 
     print("Only alphabetic letters and spaces: no") 

    #Find if string is only numeric. 
    if string.isdigits(): 
     print("Only numeric digits: yes") 
    else: 
     print("Only numeric digits: no") 
+0

當沒有定義的方法的屬性錯誤發生。 – 2015-04-04 04:46:27

回答

2

這將是不string.isdigit()string.isdigits()

>>> '9'.isdigit() 
True 
>>> '9'.isdigits() 
Traceback (most recent call last): 
    File "<pyshell#5>", line 1, in <module> 
    '9'.isdigits() 
AttributeError: 'str' object has no attribute 'isdigits' 
>>>