pythonnonetypeoperands

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


I have a list with a single value in it, populated in one function. I have another function that I want to take that value, divide it by 2, and place it into another list.

I've found similar issues, but none seem to be exactly the same as mine and the fixes don't seem to work for my issue.

from random import randint
import random

finalList = [None] * 100
firstList = [None] * 30
secondList = []

def funcOne():
    global firstList
    
    for b in range(1):
        firstList.append(random.randrange(11,12,1))

    return firstList


def funcTwo():
    global finalList

    finalList[0] = firstList
    for i in firstList:
        secondList.append(i/2)
    finalList[1] = 5 + secondList

    return finalList
    print(finalList)

funcOne()
funcTwo()

I'm receiving the:

Exception has occurred: TypeError
unsupported operand type(s) for /: 'NoneType' and 'int'
  File "C:\Users\redy\OneDrive\Documents\RPG\Biographies\TLoE_Codes\from random import randint.py", line 22, in funcTwo
    secondList.append(i/2)
  File "C:\Users\redy\OneDrive\Documents\RPG\Biographies\TLoE_Codes\from random import randint.py", line 29, in <module>
    funcTwo()
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Solution

  • firstList starts out containing thirty copies of None.

    Then in funcTwo() when you do this:

    for i in firstList:
        secondList.append(i/2)
    

    The first value in firstList is None, so this code tries to calculate None/2, which is an error.