Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Takes No Arguments
#1
class Car:
	"""A simple attempt to represent a car."""

	def __int__(self, make, model, year):
		"""Initialize the car."""
		self.make = make
		self.model = model
		self.year = year
		self.odometer_reading = 0

	def get_descriptive_name(self):
		"""Return a description of the car in a neat format."""
		long_name = f"{self.year} {self.make} {self.model}"
		return long_name.title()

	def read_odometer(self):
		"""Creates a statement of the car mileage."""
		print(f"This car has {self.odometer_reading} miles on it.")

	def update_odometer(self, mileage):
		"""Create a method for updating the mileage of the odometer."""
		if mileage >= self.odometer_reading:
			self.odometer_reading = mileage
		else:
			print(f"You can't roll back an odometer.")

	def increment_odometer(self, miles):
		"""Defines the increment increase of the odometer."""
		self.odometer_reading += miles

#Next we will define the child class, 'ElectricCar.#

class ElectricCar(Car):
	#We care creating a child class called 'ElectricCar' from the parent class 'Car'#
	"""Represents aspects of a car, specific to electric vehicles."""

	def __int__(self, make, model, year):
		"""Initialize attributes of the parent class."""
		super().__int__(make, model, year)


my_tesla = ElectricCar('tesla', 'model s', 2019)
print(my_tesla.get_descriptive_name())
Error:
Traceback (most recent call last): File "C:\Users\Carlo\Documents\Python Material\python_work\parent_child_classes.py", line 45, in <module> my_tesla = ElectricCar('tesla', 'model s', 2019) TypeError: ElectricCar() takes no arguments
***1st time posting so I hope I got the rules right, but I can't for the life of me figure out why I can't see my classes displayed. Any help would be mucho appreciato, mr./mrs./ms. roboto.***
Reply
#2
code has changed, error line number doesn't match code.
Please post code with corresponding error trace.
Reply
#3
You have typo on this lines:
    def __int__(self, make, model, year):
        """Initialize attributes of the parent class."""
        super().__int__(make, model, year)
it should be
    def __init__(self, make, model, year):
        """Initialize attributes of the parent class."""
        super().__init__(make, model, year)
also in Car class:
def __int__(self, make, model, year):
should be
def __init__(self, make, model, year):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
class Car:
	"""A simple attempt to represent a car."""

	def __int__(self, make, model, year):
		"""Initialize the car."""
		self.make = make
		self.model = model
		self.year = year
		self.odometer_reading = 0

	def get_descriptive_name(self):
		"""Return a description of the car in a neat format."""
		long_name = f"{self.year} {self.make} {self.model}"
		return long_name.title()

	def read_odometer(self):
		"""Creates a statement of the car mileage."""
		print(f"This car has {self.odometer_reading} miles on it.")

	def update_odometer(self, mileage):
		"""Create a method for updating the mileage of the odometer."""
		if mileage >= self.odometer_reading:
			self.odometer_reading = mileage
		else:
			print(f"You can't roll back an odometer.")

	def increment_odometer(self, miles):
		"""Defines the increment increase of the odometer."""
		self.odometer_reading += miles
		"""The '+=' symbology has been confusing but I tend to think of it as, ex) 'a += b'' is == to 'a = a + b'.  So in this example, on line 29, the semantic readthrough could be interpreted as the 'self.odometer_reading = self.odometer_reading + miles'."""

#When creating a child class, the parent class must be part of the current file and must appear before the child class in the file.#

#Next we will define the child class, 'ElectricCar.#

class ElectricCar(Car):
	#We are creating a child class called 'ElectricCar' from the parent class 'Car'#
	"""Represents aspects of a car, specific to electric vehicles."""

	def __int__(self, make, model, year):
		"""Initialize attributes of the parent class."""
		super().__int__(make, model, year)


my_tesla = ElectricCar('tesla', 'model s', 2019)
print(my_tesla.get_descriptive_name())
Error:
Traceback (most recent call last): File "C:\Users\Carlo\Documents\Python Material\python_work\parent_child_classes.py", line 45, in <module> my_tesla = ElectricCar('tesla', 'model s', 2019) TypeError: ElectricCar() takes no arguments
I apologize about the previous post, I had some comments in there that I thought weren't relevant and took them out without considering the line changes.

Thanks for the help people; Larz60+ for help posting questions properly and to buran for spotting that silly mistake.
Reply
#5
Dunno if this might still be of actuality for you but pay attention to the way you wrote the __init__ method.
You wrote it __int__ instead of __int__ thus making your class unable to accept arguments
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class takes no arguments Nazartfya 2 920 Jun-27-2020, 12:45 PM
Last Post: Nazartfya
  Why Car() takes no arguments louis216 2 731 Jun-25-2020, 03:16 AM
Last Post: louis216
  random.choice() takes two positional arguments, but three were given. ShakeyPakey 5 2,425 May-31-2020, 03:13 PM
Last Post: deanhystad
  Question() takes no arguments jwrcfv 2 1,000 Apr-02-2020, 06:08 PM
Last Post: jwrcfv
  add() takes 2 positional arguments but 3 were given Man_from_India 3 2,128 Feb-10-2020, 05:08 PM
Last Post: Man_from_India
  Class takes no arguments Myang123 3 6,159 Nov-26-2019, 12:01 PM
Last Post: Davy_Jones_XIV
  This constructor takes no arguments Friend 2 3,109 Jun-26-2019, 02:54 PM
Last Post: Friend
  takes 21 positional arguments but 24 were given sophsoph 5 1,732 Jun-21-2019, 12:53 AM
Last Post: ichabod801
  Why Person() takes no arguments dianefly 3 26,846 Oct-22-2018, 04:15 PM
Last Post: dianefly
  Giving class multiple arguments AndyArsalan 1 1,736 Oct-04-2018, 11:25 PM
Last Post: ODIS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020