Advertisement
Kamend1

Vehicle

Oct 27th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. class Vehicle:
  2.     def __init__(self, type_vehicle: str, model: str, price):
  3.         self.type_vehicle = str(type_vehicle)
  4.         self.model = str(model)
  5.         self.price = price
  6.         self.owner = None
  7.  
  8.     def buy(self, money: int, owner: str):
  9.         if money >= self.price and self.owner is None:
  10.             change = money - self.price
  11.             self.owner = owner
  12.             return f"Successfully bought a {self.type_vehicle}. Change: {change:.2f}"
  13.         elif money < self.price:
  14.             return "Sorry, not enough money"
  15.         else:
  16.             return "Car already sold"
  17.  
  18.     def sell(self):
  19.         if self.owner is not None:
  20.             self.owner = None
  21.         else:
  22.             return "Vehicle has no owner"
  23.  
  24.     def __repr__(self):
  25.         if self.owner is not None:
  26.             return f"{self.model} {self.type_vehicle} is owned by: {self.owner}"
  27.         else:
  28.             return f"{self.model} {self.type_vehicle} is on sale: {int(self.price)}"
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement