+ 1

New Learner to Python: TypeError: __init__() missing 1 required positional argument: 'x'

Hi all, I have only just started my Python learning journey. I have got the above error message to my code I am trying to write up. For the life of me, I cannot figure out where I am going wrong, would someone be a star and help me out! I'm using Sublime Text and the code is here below. Thank you in advance! class myClass: def __init__(self, x): self.x = x #Returns x. def getX(self): return self.x #Changes x. def setX(self, newX): self.x = newX #Multiplies x by 20 and saved to y #Reduces y by 5 #Prints y def function1(self): self.y = self.x * 20 self.y = - 5 print(y) #prints the result of: number3 not equal to number2 and number1 less than number2 def function2(self): number1 = 12 number2 = 42 number3 = 68 print(number3 != number2 and number1 < number2) #prints a greeting def function3(self): print("Hello there!") #Create an object of our class class1 = myClass() #Change x to the number 5 class1.setX("5") #Call each function class1.function1() class1.function2() class1.function3() Error message: Traceback (most recent call last): File "C:\Users\teams\Downloads\Part 1 (2).py", line 43, in <module> class1 = myClass() TypeError: __init__() missing 1 required positional argument: 'x' [Finished in 111ms]

11th Jul 2021, 8:05 AM
Julia Smith
2 Answers
+ 6
When you create the class1 variable you're missing the 'x' parameter. In the MyClass init method, it shows that 2 parameters are needed: "self" and "x". Since it's a class, the "self" attribute refers to the class itself, so no need to include it when creating an object. If anything else is there you need to include it though. class1 = myClass(7) # for example Julia Smith No problem! I appreciate you asking a full question, with your code attached and bonus points for the error message included! Ask like this anytime and we'd all be glad to help.
11th Jul 2021, 8:12 AM
Slick
Slick - avatar
+ 1
Hi Slick, thank you so much! Thats really helpful, and I got there, I think. At least no errors came up! Thank you again <3
11th Jul 2021, 11:26 AM
Julia Smith