-1

I need to write a class, Numbers, with methods addNumbers and currentSum, that prints the current sum. Eg.:

numbers = Numbers()
numbers.addNumber(3)
numbers.addNumber(2)
print numbers.currentSum()

should print 5

I have made a class:

class Numbers:
    def __init__(self, numbers):
    self.numbers=addNumbers
    return numbers.currentSum()
Numbers=Numbers()

A hint, anyone?

1
  • If you're using Python 2.x, use class X(object): instead. class X: is an old style class.. Here are the differences. Commented Dec 8, 2011 at 16:56

3 Answers 3

4
class Numbers:
    def __init__(self):
       self.__sum=0
    def addNumber(self, number):
       self.__sum += number
    def currentSum(self):
       return self.__sum
Sign up to request clarification or add additional context in comments.

1 Comment

That's... not a hint. That's doing the entire thing for the OP.
2

Hints:

  1. You need to implement the currentSum() method
  2. You also need to implement the addNumber() method
  3. addNumber() will need to keep track of numbers across method calls. Use a list.

More reading on methods: What is a "method" in Python?

Comments

0
class Numbers(object):
    def __init__(self):
        self.sum = 0

    def addNumber(self, n):
        self.sum += n

    def currentSum(self):
        return self.sum

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.