2015-04-04 3 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

Ошибка атрибута происходит, когда метод не определен. –

ответ

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' 
>>> 
Смежные вопросы