0

I have the class planets that stores in a dictionary Key which is the name of the planet, and value which is the size of the planet, and then add all the values in the dictionary and print it

this is my class

 class planets:
    def store(self, name, mass):
        aDict[self.name] = self.mass

    def add(self):
        return sum(aDict.values())


planets.store("Sun",50)

planets.store("Mercury",7)

print(planets.add())

I am expecting to get an answer of 57, however I am getting an error which is:

TypeError: store() missing 1 required positional argument: 'mass'

How to fix that problem?

Mozein
  • 787
  • 5
  • 19
  • 33
  • `aDict[self.name] = self.mass` would fail as there are no name and mass fields. – Basilevs Apr 03 '15 at 03:24
  • possible duplicate of [Pygame TypeError: missing 1 required positional argument:](http://stackoverflow.com/questions/19034714/pygame-typeerror-missing-1-required-positional-argument) – Basilevs Apr 03 '15 at 03:34

1 Answers1

3

planets is a class, def store(self, name, mass): is a instance method. You need to make a instance of planets first to use it's instance methods.

p = planets()
p.store("Sun",50)
p.store("Mercury",7)
print(p.add())

Second problem, you have to use mass to refer the parameter mass not self.mess. self.mess is a property of planets object. And, you need to initialize the aDict property in the __init__ method. So you can define the class using below codes.

class planets:
    def __init__(self):
        self.aDict = {}

    def store(self, name, mass):
        self.aDict[name] = mass

    def add(self):
        return sum(self.aDict.values())
Yang
  • 204
  • 2
  • 9
  • You are right, However I am still getting error which is : " AttributeError: 'planets' object has no attribute 'mass' " – Mozein Apr 03 '15 at 03:30
  • The error actually sounds like invoking an instance method without an instance. Python would consider "Sun" to be self, 50 to be name and complain about missing the last (mass) argument. – vladris Apr 03 '15 at 03:31
  • @Mozein: Se my comment to question. You are referencing absent fields in store() method. Use method arguments instead. Remove "self.". – Basilevs Apr 03 '15 at 03:35