TypeError: method takes 0 positional arguments but 1 was given in Python
Hi guys.
I just recently started coding at my school and am learning to use Python. And I created an object class CustomerInfo
as below:
class CustomerInfo:
def __init__ ():
self.name = name
self.order = order
self.quantity = quantity
self.address = address
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 quantity: "))
address = input("Enter your address: ")
customerObj = CustomerInfo()
print ( "Name: ", customerObj.name)
print ( "Order: ", customerObj.order)
print ( "Quanity: ", customerObj.quantity)
print ( "Address: ", customerObj.address)
I want to take input some information of customers like name, order, quantity and address but I get an exception TypeError: __init__() takes 0 positional arguments but 1 was given.
Enter your name: Python
Enter your order: 23
Enter your quantity: 21
Enter your address: 333 West USA
Traceback (most recent call last):
File "main.py", line 32, in <module>
customerObj = CustomerInfo()
TypeError: __init__() takes 0 positional arguments but 1 was given
How can I fix it? Anyone can explain to me.
Thanks for any suggestions.
-
L0
Leonardo Urbano Sep 05 2021
Method
__init__ ()
should be acceptedself
in Python because, well, it's a method.Change:
def __init__ ():
TO:
def __init__ (self):
And it solved the issue for you.
-
W0
Watcharapong Jakkawannorasing Sep 05 2021
Hey, I think this is the message throw when you forget
(self,...)
arg in__init__()
or any other method.Add
self
param and it is solved for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.