Here I have created a Car Class. I have also created another class called Battery and then a sub class ElectricCar that takes from the Car class. I then begin to create an Instance Tesla then I connect it to the Car class, at this point everything is fine. However when I write Tesla.upgrade() for some reason Pycharm/Python does not recognise the function, does anyone know why this is I dont want to remove the subclasses.
class Car:
"""A simple attempt to represent 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_reading = 0
def get_descriptive_name(self):
"""Return a neatly formatted descriptive name."""
long_name = f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
"""Print a statement showing the car's mileage."""
print(f"This car has {self.odometer_reading} miles on it.")
def update_odometer(self, mileage):
"""Set the odometer reading to the given value."""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""Add the given amount to the odometer reading."""
self.odometer_reading += miles
class Battery:
def __init__(self, battery_size=40):
"""Initialize the battery's attributes."""
self.battery_size = battery_size
def describe_battery(self):
"""Print a statement describing the battery size."""
print(f"This car has a {self.battery_size}-kWh battery.")
def get_range(self):
"""Print a statement about the range this battery provides."""
if self.battery_size == 40:
range = 150
elif self.battery_size == 65:
range = 225
print(f"This car can go about {range} miles on a full charge.")
def upgrade_battery(self):
"""Upgrade the battery if possible."""
if self.battery_size == 40:
self.battery_size = 65
print("Upgraded the battery to 65 kWh.")
else:
print("The battery is already upgraded.")
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
"""
Initialize attributes of the parent class.
Then initialize attributes specific to an electric car.
"""
super().__init__(make, model, year)
self.battery = Battery()
Tesla = Car("Telsa", "Cybertruck", "2024")
Tesla.upgrade_battery
The Code is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1