Reputation: 21
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'
Upvotes: 0
Views: 5505
Reputation: 33335
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.
Upvotes: 1
Reputation: 1629
You append to firstList
, a list starting with 100 Nones. The first 100 elements will yield None/2. Initialize it as firstList = []
.
Also, what do you suppose for b in range(1):
does?
Upvotes: 0
Reputation: 20450
During iteration you assigned i = None
,
and then tried to compute the expression None / 2
.
Don't do that, as it won't work, it will give the TypeError you reported.
Prefer to assign a numeric quantity to i
, and then divide it by 2.
Upvotes: 0