简体   繁体   中英

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!!!

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!")

You should ensure that your USDCheckBalance method does return an int. It seems that there are cases where it doesn't return anything, causing the error. (The "you don't have an account" case)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM