10

I have just recently started to code at my school and I am learning how to use Python. Our teacher gave us this task:

  1. Create a class and give it the name “CustomerInfo”.
  2. Create a constructor with no parameters ( only self ).
  3. Create user inputs for name, order, quantity, and address.
  4. Create mutator methods for name, order, quantity, and address.
  5. Create accessor methods for name, order, quantity, and address.
  6. Create a new CustomerInfo() object and call it “customer1”.
  7. Print out the customer information.

Here are my codes:

class CustomerInfo:

    def __init__ ():
        self.name = theName
        self.order = theOrder
        self.quantity = theQuantity
        self.address = theAddress

    def setName( self, newName ):
        self.Name = newName
    def setOrder ( self, newModel ):
        self.model = newModel
    def setQuantity ( self, newQuantity ):
        self.quantity = newQuantity
    def setAddress (self, newAddress ):
        self.address = newAddress

    def getName ( self ):
        return self.name
    def getOrder ( self ):
        return self.order
    def getQuantity ( self ):
        return self.quantity
    def getAddress ( self ):
        return self.address

name = input("Enter your name: ")
order = input("Enter your order: ")
quantity = int(input("Enter your quanity: "))
address = input("Enter your address: "))

customer1 = CustomerInfo()

print ( "Name: ", customer1.name)
print ( "Order: ", customer1.order)
print ( "Quanity: ", customer1.quantity)
print ( "Address: ", customer1.address)

However, I got the following Error:

TypeError: __init__() takes 0 positional arguments but 1 was given

I added (self) to __init__ as described in the comments, and now when I run the module the inputs work but after I put the inputs of name, order, quantity, and address, the outcome came out like this:

Traceback (most recent call last):
File line 32, in <module>
    customer1 = CustomerInfo()
File line 4, in __init__
    self.name = theName
NameError: name 'theName' is not defined
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Zach Maret
  • 101
  • 1
  • 1
  • 3
  • 9
    Yes, this is the message you get when you forget `(self,...)` arg to `__init__()` or any other method. – smci Sep 18 '14 at 01:16
  • If intuitively you think `__init__` shouldn't take any arguments, then note that `__init__` does not create a new instance of a class; [that's done by `__new__` instead](http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init). But you probably don't need to define your own `__new__`. – z0r Sep 18 '14 at 01:31
  • If you are not using Python 3 (which isn't clear from the question), your class should inherit from `object`, ie `class CustomerInfo(object):` – sapi Sep 18 '14 at 01:55
  • @sapi That's really the least of his worries. – David Robinson Sep 18 '14 at 01:56
  • @sapi: The use of `input` vs. `raw_input` suggests *Python 3*. – khampson Sep 18 '14 at 01:58
  • @KenHampson That's an optimistic way to read that; I've had plenty of students use it in Python 2 because it's what Google suggests ;) – sapi Sep 18 '14 at 01:59
  • @sapi: Ah, fair enough. =) – khampson Sep 18 '14 at 02:01
  • As well as the use of `input()`, Zach's also using `print()`, but I guess that's _still_ not definitive proof of Python 3. – PM 2Ring Sep 18 '14 at 06:13

3 Answers3

30

The __init__ method needs to accept self in Python, because, well, it's a method.

Change:

def __init__ ():

To:

def __init__ (self):
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
1

I don't want to provide the exact code for you, since this is homework, and you'll learn better if you arrive at the correct code yourself. I will point out the two main points (after adding self, which has already been mentioned and you have added) that should allow you to proceed with writing the code yourself:

  1. The instructions say the constructor should take no parameters other than self. This means there are no arguments to the function, so you can't refer to things like theName inside of the function. That is looking for essentially a global variable, which should rarely, if ever, be used.
  2. You aren't making use of the getter and setter (referred to in your instructions as accessor and mutator) functions the instructions asked you to use. These are key to proceeding given a constructor which takes no arguments.
khampson
  • 14,700
  • 4
  • 41
  • 43
0

Yes, this is the message you get when you forget (self,...) arg to __init__() or any other method.

smci
  • 32,567
  • 20
  • 113
  • 146