获取错误的结果Python

时间:2014-11-23 23:29:28

标签: python

当我运行我的代码时,我一直得到错误的结果。我试图得到差异,并将其设置为对象的新颜色,如果两个对象颜色不相等。如果两种颜色相等,我将该颜色用作新颜色。我不知道我失踪了什么。

代码:

    class DooDad :
    _serialNumber = 1000

    def __init__(self, color, weight, value):
        self._color = color
        self._weight = weight
        self._value = value
        DooDad._serialNumber = DooDad._serialNumber + 1
        self._serialNumber = DooDad._serialNumber

    def getColor(self):
        return self._color

    def getWeight(self):
        return self._weight

    def getValue(self):
        return self._value

    def getSerialNumber(self):
        return self._serialNumber

    def __add__(self, other):
        if self._serialNumber - other._serialNumber == 1 or other._serialNumber - self._serialNumber == 1:
            newValue = self._value * other._value
        else:
            newValue = self._value + other._value

        maxWeight = max(self._weight, other._weight)
        leftColor = self._color
        return DooDad(self._color, maxWeight, newValue)

    def __sub__(self, other):
        leftColor = self._color
        rightWeight = other._weight
        minValues = min(self._value, other._value)
        return DooDad(leftColor, rightWeight, minValues)

    def __mul__(self, other):  
        maxWeight = max(self._weight, other._weight)
        avgValue = (self._value + other._value) / 2
        if self._color != other._color:
            colorNotIn = self._notInEither(self._color, other._color)
            return DooDad(colorNotIn, maxWeight, avgValue)

        return DooDad(self._color, maxWeight, avgValue)    


    def _notInEither(self, color1, color2):
        allColors = {"red", "blue", "green"}
        colors = {color1, color2}
        print(colors)
        colorNotIn = allColors.difference(colors)
        return "".join(colorNotIn)


    def __repr__(self):
        return " %s DooDad weighing %.0f grams. Value = %f points. S/N= %i" % (self._color, self._weight, self._value, self._serialNumber)  


if __name__ == "__main__":
    doodads = []
    doodads.append(DooDad("red",5,10))
    doodads.append(DooDad("red",8,9))
    doodads.append(DooDad("blue",20,15))
    doodads.append(DooDad("green",2,5))
    doodads.append(doodads[0] + doodads[1])
    doodads.append(doodads[2] + doodads[0])
    doodads.append(doodads[3] - doodads[1])
    doodads.append(doodads[1] - doodads[3])
    doodads.append(doodads[0] * doodads[1])
    doodads.append(doodads[0] * doodads[2])
    doodads.append(doodads[0] * doodads[4]) #blue but i get red
    doodads.append(doodads[3] * doodads[4]) #red but i get blue



    for doodad in doodads:
        print(doodad)

1 个答案:

答案 0 :(得分:0)

我认为你只是在测试你的班级错误; Example[x]应为doodads[x]

doodads = []
doodads.append(Example("red",5,10))
doodads.append(Example("red",8,9))
doodads.append(Example("blue",20,15))
doodads.append(Example("green",2,5))
doodads.append(doodads[0] * doodads[3]) #blue
doodads.append(doodads[3] * doodads[4]) #red

输出:

red  weighing 5 grams. Value = 10.000000 points. S/N= 1001
red  weighing 8 grams. Value = 9.000000 points. S/N= 1002
blue  weighing 20 grams. Value = 15.000000 points. S/N= 1003
green  weighing 2 grams. Value = 5.000000 points. S/N= 1004
blue  weighing 5 grams. Value = 7.000000 points. S/N= 1005
red  weighing 5 grams. Value = 6.000000 points. S/N= 1006