When I construct a __init__()
in a Car class, say, I want to check that variables make
and current_gas
to be string and float or integer (not negative), respectively.
Then how do I raise an appropriate error for each variable?
I tried
class Car:
def __init__(self,make,current_gas):
if type(make) != str:
raise TypeError("Make should be a string")
if not type(current_gas) == float or type(current_gas) == int:
raise TypeError("gas amount should be float or int")
if current_gas <=0:
raise ValueError("gas amount should not be negative")
However, this __init__()
is not working properly. How do I fix it?