1

When I run the below code, I get

AttributeError: monster instance has no attribute 'D'

What would I have to change so that Godzilla.D would return (in this case, with 2,3 as the params) 7?

def add(x):
    y = x + 1
    return y

class monster:
    def __init__(self, A, B):
        self.A = A
        self.B = B
        C = A * B
        D = add(C)

Godzilla = monster(2,3)
Godzilla.D
timgeb
  • 76,762
  • 20
  • 123
  • 145
Cro2015
  • 193
  • 1
  • 2
  • 8

1 Answers1

4

You forgot to set D as an attribute of the instance of monster being initialized in the __init__ method. Change D = add(C) to self.D = add(C).

timgeb
  • 76,762
  • 20
  • 123
  • 145