Create a class Vehicle
with private attributes for brand, model, and price. Implement a discount method that applies a given percentage discount to the price, making sure the discount cannot exceed a certain limit (e.g., 50%).
Example 1:
car = Vehicle("Toyota", "Camry", 20000) car.apply_discount(40) print(car.get_price()) Output: 12000.0
Example 2:
car = Vehicle("Honda", "Civic", 18000) car.apply_discount(60) print(car.get_price()) Output: 9000.0
Limit the discount percentage within the allowed range and then apply it to the price.
<pre>
class Vehicle:
def __init__(self, brand, model, price):
self._brand = brand
self._model = model
self._price = price
def apply_discount(self, percentage):
percentage = min(percentage, 50) # Limiting to 50%
self._price -= self._price * percentage / 100
def get_price(self):
return self._price
# Test the class
car = Vehicle(“Toyota”, “Camry”, 20000)
car.apply_discount(40)
print(car.get_price()) # Output: 12000.0
</pre>
Unlock AI & Data Science treasures. Log in!