Python Forum
Electric Car battery size updating ???? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Electric Car battery size updating ???? (/thread-20906.html)



Electric Car battery size updating ???? - ridgerunnersjw - Sep-06-2019

Hello...
I copied the following out of a book as I am trying to teach myself Python....Can someone please tell me how I can create an instance of an ECar and modify the battery size to 85? I have tried everything under the moon and have not been successful updating an instance of an ECar with this as because the get_range method ALWAYS shows 240 which is the default of 70 ...I have been able to create a Battery instance and change it but I don't believe I can then use this to update the ECar because it is a totally different class....Thank you for your answers...

See code below......................
class Car():
	"""A simple attempt to build a car"""

	def __init__(self, make, model, year):
		"""Initialize attributes to describe a car"""
		self.make = make
		self.model = model
		self.year = year
		self.odometer = 0

	def get_descriptive_name(self):
		"""Return a neatly formatted name"""
		long_name = str(self.year) + ' ' + self.make + ' ' + self.model
		return long_name.title()

	def odometer_reading(self):
		"""Read auto odometer"""
		print('This car has ' + str(self.odometer)+ ' miles on it.')

	def update_odometer(self, miles):
		"""Updates the odometer reading with miles"""	
		if miles > self.odometer:
			self.odometer = miles
		else:
			print ('You cannot roll back the odometer')	

	def increment_odometer(self, miles):
		"""Add miles to existing odometer reading"""
		self.odometer += miles

class Battery():
	"""Model of Electric Car battery"""
	def __init__(self, battery_size = 70):
		"""Initialize Battery settings"""
		self.battery_size = battery_size

	def describe_battery(self):
		"""Describes the Electric car battery"""
		print('This car has a ' + str(self.battery_size) + '-kwh battery')

	def get_range(self):
		"""Tell range of battery"""
		if self.battery_size == 70:
			range = 240
		elif self.battery_size ==85:
			range = 270 
		else:
			range = 0	

		msg = "This car can go about " + str(range)
		msg += " miles on a full charge."
		print (msg)

class ECar(Car):
	"""Create an instance of an elecric car"""
	def __init__(self, make, model, year):
		"""Initialize parent attributes"""
		super().__init__(make, model, year)
		self.battery = Battery()



RE: Electric Car battery size updating ???? - metulburr - Sep-06-2019

You would just pass the battery size to Ecar arguments, and pass that along to Battery class like this
class ECar(Car):
    """Create an instance of an elecric car"""
    def __init__(self, make, model, year, batt_size):
        """Initialize parent attributes"""
        super().__init__(make, model, year)
        self.battery = Battery(batt_size)
        
car = ECar('Tesla', 'Model X', 2016, 85)
print(car.battery.get_range())
Output:
This car can go about 270 miles on a full charge. None