I’m making a simple Bank Account class in Python. I was trying to make sure the input of set_balance() is either an int
or a float
. The balance is equal to 0 by default. The code goes as follows:
def set_balance(self, balance):
if type(balance) != int or type(balance) != float:
return
if balance < 0 or balance >= 100000:
return
self._balance = balance
However whenever I set the balance then print get_balance()
, the result is 0.
I was able to get the desired results with the following code:
def set_balance(self, balance):
if type(balance) not in [int, float]:
return
if balance < 0 or balance >= 100000:
return
self._balance = balance
I have used code like while type(num) != int:
in a different piece of code and it worked just as expected. Is there a reason why this type of code doesn’t work in a class method?