我怎么会在输出中看到“无”

问题描述 投票:0回答:2

从Udacity学习python。练习在下面提到。我看不到输出“无”来自何处。我缺少一些关于课堂的东西吗?提前谢谢

总是输出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())
python output
2个回答
1
投票

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

def getClassiness(self):
    return self.classiness

1
投票

Class Classy(object):

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

测试用例

我= Classy()

应为0

print(me.getClassiness())

© www.soinside.com 2019 - 2024. All rights reserved.