Reputation: 451
Create a class called boat. In the__init__(), allow the user to specify the following attributes: price, speed, fuel, mileage. If the price is greater than 100,000, set the tax to be 20%. Otherwise set the tax to be 18%.
How do I do a conditional in my class, saying, if price > 100,000 then tax = 20%?
Here's my code so far:
class boat(object):
def __init__(self, price, speed, fuel, mileage, tax):
self.price = price
self.speed = speed
self.fuel = fuel
self.mileage = mileage
self.tax = tax
Upvotes: 0
Views: 90
Reputation: 7
class boat:
def __init__(self, price, speed, fuel, mileage):
self.price = price
if price > 100000:
self.tax = 0.2
else:
self.tax = 0.18
self.speed = speed
self.fuel = fuel
self.mileage = mileage
self.price+=self.tax
As there is a conditional in the question, we can use conditional in the class itself so that all the objects instantiated from the class will follow the conditional and gets updated.
Upvotes: 0
Reputation: 49
class boat(object):
def __init__(self, price, speed, fuel, mileage):
self.price = price
if price > 100000:
self.tax = 0.2
else:
self.tax = 0.18
self.speed = speed
self.fuel = fuel
self.mileage = mileage
You can change the 0.2 to 1.2, for example, seeming as we are adding on tax. However I will leave that up to you as I assume that this is only a small snippet of your code so you can make the choice of what you want it to be.
Upvotes: 0
Reputation: 2480
You can do it by:
class boat(object):
def __init__(self, price, speed, fuel, mileage):
if price > 100000:
self.price = price* 1.20
else:
self.price = price* 1.18
self.speed = speed
self.fuel = fuel
self.mileage = mileage
self.tax = tax
Upvotes: 1