为什么我在输出中看到“无”

2024-05-20 20:45:27 发布

您现在位置:Python中文网/ 问答频道 /正文

从Udacity学习python。下面提到练习。我看不出输出“无”来自何方。我错过了哪些课程?提前Thx

输出总是 0 没有

=======代码开始============================

"""You can use this class to represent how classy someone
or something is.
"Classy" is interchangable with "fancy".
If you add fancy-looking items, you will increase
your "classiness".
Create a function in "Classy" that takes a string as
input and adds it to the "items" list.
Another method should calculate the "classiness"
value based on the items.
The following items have classiness points associated
with them:
"tophat" = 2
"bowtie" = 4
"monocle" = 5
Everything else has 0 points.
Use the test cases below to guide you!"""

class Classy(object):

    def __init__(self):
        self.items = []
        self.classiness = 0

    def getClassiness(self):
        print(self.classiness)

    def createList(self):
        self.items.append(item)

    def addItem(self, item):

        if item=="tophat":
            self.classiness+=2
        elif item=="bowtie":
            self.classiness+=4
        elif item=="monocle":
            self.classiness+=5
        else:
            self.classiness+=0

        return self.classiness

# Test cases
me = Classy()

# Should be 0
print(me.getClassiness())

Tags: thetoselfyouistophatdefwith
2条回答

类Classy(对象):

def __init__(self):
    self.items = []
    self.classiness = 0

def getClassiness(self):
    return self.classiness

def createList(self):
    self.items.append(item)

def addItem(self, item):

    if item=="tophat":
        self.classiness+=2
    elif item=="bowtie":
        self.classiness+=4
    elif item=="monocle":
        self.classiness+=5
    else:
        self.classiness=0

测试用例

我=优雅的()

应该是0

打印(me.getClassiness())

您的方法getClassiness()正在打印,调用方也在打印。 也许你想返回一个值而不是打印

def getClassiness(self):
    return self.classiness

相关问题 更多 >