fr3oh fr3oh - 4 months ago 39
Python Question

Python - TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

I have a program that allows a user to convert between currencies and allow them to have bank accounts for five different currencies.

When I try to display the bank account balance to the user, I get a message saying that 'value' is a

Nonetype
, yet I don't understand how
None
is being returned.

print("Check balance of which account?")
print("1. USD | 2. EUR | 3. JPY | 4. GBP | 5. RUB")
acntaction = input()
if acntaction == "1":
if player.USDhasBankAccount == True:
value = bank.USDCheckBalance(player.USD)
print("Bank Account Balance: " + str(value * currencies[0].getVal()) + " " + currencies[0].getCurName() + ".") # prints bank account balance to player
else:
print("You do not have a USD bank account!")


Here is the USDCheckBalance method:

def USDCheckBalance(self, USDaccountName):
i = 0
while i < len(self.USDaccountList):
USDaccountCheck = self.USDaccountList[i].owner
if USDaccountName == USDaccountCheck:
USDaccount = self.USDaccountList[i]
return USDaccount.value
break
else:
i = i + 1
print("You don't have an account!")


There is a list created when the user opens their USD bank account and there is only one. Please let me know how I can resolve this!!!

Answer

If there is no bank account, you implicitly return None. Best use exceptions in that case:

def USDCheckBalance(self, USDaccountName):
    for account in self.USDaccountList:
        if USDaccountName == account.owner:
            return account.value
    raise KeyError("You don't have an account!")

and

print("Check balance of which account?")
print("1. USD | 2. EUR | 3. JPY | 4. GBP | 5. RUB")
acntaction = input()
if acntaction == "1":
    try:
        value = bank.USDCheckBalance(player.USD)
        print("Bank Account Balance: {} {}.".format(value * currencies[0].getVal(), currencies[0].getCurName())
    except KeyError:
        print("You do not have a USD bank account!")