Take the 2-minute tour ×

I am trying to write a class that stores a person's name and number.

class Contact(object)
    def __init__(self, name, number)
        self._name = name
        self._number = number

    def getNumber(self):
        return self._number

    def setNumber(self, newNumber):
        self._number = newNumber

    def numberUpdate(self):
        newNumber = number

    def action(self):

        person = Contact("Jonathan")

        number = input("What's your number: ")

person = Contact()

It prompts me for the number as I would hope. But after I enter the number, it keeps giving me this error:

NameError: global name 'number' is not defined

Can anybody tell me what I did wrong here? I tried:

 def numberUpdate(self, number):
    newNumber = number

def action(self):

    person = Contact("Jonathan")

    number = input("What's your number: ")

and it gives me:

TypeError: numberUpdate() takes exactly 2 arguments (1 given)

Any help would be appreciated. Thank you.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

General suggestions

The design of your Contact class is broken in quite a few ways (for example, the action() method should not be instantiating a new Contact object within the Contact class).

It looks like your Contact class is meant to store one name and a number with that name. You should not be specifying a specific name within the action() method, you should instead allow the class to store anybody's name. Handle the storing of a name in the __init__ method, or with setter methods.

Also, you should really rename your action() method - the name gives no hint whatsover as to what its result will be. Your syntax on calling the action() method is wrong too.

Overarching problem

Basically, you're having problems because you're not understanding some of the basics of object-oriented programming. Instead of struggling through each error, I'd recommend taking some time to read up on how objects and classes work.

share|improve this answer
I understand that I should not be initiating a new instance within the object class. However, I am trying to test something here. That is why I am doing it and also inputting a name by myself. I understand the basics of it using a getter and setter method. My main objective of this is to understand how to pass a variable within each method. I would appreciate it if you can guide me here. –  Nutty Nur May 9 '12 at 2:32
I'm happy to guide you, but trust me when I say there are so many errors with your code that you need a thorough re-visit of how classes work. To "pass a variable within each method," you should probably just store the variable in a field. You can also call a method that returns a variable. –  David Cain May 9 '12 at 6:11
Also, to be clear, creating a Contact object with the name "Jonathan" is a perfectly valid way of testing. This just belongs outside of the class. –  David Cain May 9 '12 at 6:14
Ok thank you. I will try and do what you suggested. Many thanks. –  Nutty Nur May 9 '12 at 7:16
Hi David, I have attempted what you have said and moved my "method" into a class in another file. It works much better and is much cleaner now. Just wanted to say thank you. Cheers! –  Nutty Nur May 9 '12 at 15:41

Your Answer


