TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

Hey

Can someone please explain why i ping this error when returning the ‘elif’ part of the statement.
Other research has helped me resolve a similar error when concatenating strings and int’s together.

But i do not understand why i would get a type error when i’m only printing a string?
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
appreciate any support.

import random
money = 100

# -----------------------Heads and Tails-------------------------------
#returns operand type error when it fails???

def coin_flip(guess, bet):
  print ("You are playing Heads or Tails, you have choosen", guess)
  randomNumber = random.randint(1, 2)
  # headOrTails varible created to allow streamlining of 'if' statement and inclusion into a concatenated statement
  headOrTails = 'heads' if randomNumber == 1 else 'tails'
  # guessConvert removes failure risk due to user input error.  e.g Heads, HEaDS HEADS etc
  guessConvert = guess.lower()

  if guessConvert == 'heads' and randomNumber == 1 or guessConvert == 'tails' and randomNumber == 2:
    print("Its", headOrTails)
    print("Congratulations you won:",str(bet))
    return bet
  elif not guessConvert == 'heads' or guessConvert == 'tails':
    print("Error! you must enter either Heads or Tails to play")
  else:
    print("You Lose!")
    return -bet
#I am purposely trying to fail the function, to imitate a user input error
money += coin_flip("Head", 10)

The problem isn’t related to print. When the elif clause runs, your function doesn’t return anything. So you get None (the absence of a return value):

def example():
   pass

print(example()) // should show None

None(Type) + integer results in an error. Python is not capable of adding these two types together.

1 Like

Ahhh yes of course, thanks so much.