我是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")
答案 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'
>>>