AttributeError:' str'对象没有属性isalpha()

时间:2015-04-04 04:40:43

标签: python string python-3.x attributeerror

我是python的新手,我遇到了字符串问题。我得到“AttributeError:'str'对象没有属性”,但我为此感到困惑。我提供了一些代码,所以任何建议都会有所帮助!

#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")

1 个答案:

答案 0 :(得分:3)

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'
>>>