[英]Unresolved attribute reference in Python 3
class StringHandling:
def __init__(self,yo):
self.yo = yo
def my_first_test():
yo = input("Enter your string here")
ya = (yo.split())
even = 0
odd = 0
for i in ya:
if len(i) % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("The number of odd words are ", odd)
print("The number of even words are", even)
if __name__ == '__main__':
c = StringHandling(yo="My name is here ")
c.my_first_test()
这里有什么问题? 尝试了一切! 我尝试了缩进并创建了对象,但是对象c未调用方法my_first_test。
你很亲密 尝试这个:
class StringHandling(object):
def __init__(self,yo):
self.yo = yo
def my_first_test(self):
yo = input("Enter your string here: ")
ya = (yo.split())
even = 0
odd = 0
for i in ya:
if len(i) % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("The number of odd words are ", odd)
print("The number of even words are", even)
if __name__ == '__main__':
c = StringHandling(yo="My name is here ")
c.my_first_test()
看看这个灵感: https : //jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
注意我所做的更改:
yo = input("Enter your string here")
并格式化文本字符串 my_first_test
方法 self
-在这里阅读为什么会这样: 自我的目的是什么? 结果
python3 untitled.py
Enter your string here: a ab a
The number of odd words are 2
The number of even words are 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.