Python Forum
Electric Car battery size updating ????
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Electric Car battery size updating ????
#1
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()
Reply
#2
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
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Total electric construction cost project SecondSandwich94 2 2,113 Jul-21-2021, 09:37 PM
Last Post: deanhystad
  Reading Smartphone Battery Level via Bluetooth maxwell 1 2,127 Jun-17-2020, 12:48 AM
Last Post: Larz60+
  size of set vs size of dict zweb 0 2,117 Oct-11-2019, 01:32 AM
Last Post: zweb
  Writing device driver to stop electric supply to any I/O port sumandas89 0 1,768 May-02-2019, 10:22 AM
Last Post: sumandas89
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,434 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb

Forum Jump:

User Panel Messages

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