0% found this document useful (0 votes)
8 views

PYTHON 21

Uploaded by

ANJALI MODI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

PYTHON 21

Uploaded by

ANJALI MODI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM 21

WAP CREATING A CAR CLASS WITH ATTRIBUTES MAKE, MODEL, YEAR, COLOR AND
INSTANCE METHODS -START, ACCELERATE, BRAKE AND STOP.

SOURCE CODE:
class Car:
def __init__(self,make,model,year,color):
self.make = make
self.model = model
self.year = year
self.color = color
self.started = False
self.speed = 0
self.max_speed = 200
def display_info(self):
print(f"Make: {self.make}")
print(f"Model: {self.model}")
print(f"Year: {self.year}")
print(f"Color: {self.color}")
def start(self):
if not self.started:
print("Car started.")
self.started = True
else:
print("Car is already started.")
def stop(self):
if self.started:
print("Car stopped.")
self.started = False
self.speed = 0
else:
print("Car is already stopped.")
def accelerate(self,target_speed):
if self.started:
if target_speed <= self.max_speed:
self.speed = target_speed
print(f"Accelerate to {self.speed} km/h.")
else:
print(f"Cannot accelerate beyond {self.max_speed} km/h.")
def brake(self, target_speed):
if self.started:
if target_speed >= 0 and target_speed <= self.speed:
self.speed = target_speed
print(f"Car slowed down to {self.speed} km/h.")
elif target_speed < 0:
print("Invalid speed. Cannot brake.")
else:
print("Car cannot be slowed down further.")

car = Car(make="Toyota", model="Camry", year=2022, color="Blue")


print("Car information:")
car.display_info()
print("\t")
car.start()
car.accelerate(100)
car.accelerate(250)
car.brake(40)
car.brake(60)
car.stop()

You might also like