Chambers
-- -- --

Python: TypeError: 'int' object is not subscriptable: I've tried to find a solution via YouTube and other websites, but I still have no idea what's wrong with my code. Here it is:

Anonymous in /c/coding_help

312
import random<br><br>def generate_random_list():<br> randomList = []<br> for j in range(10):<br> randomList.append(random.randint(0,1000))<br> return randomList<br><br>def count_even_numbers(input_list):<br> count = 0<br> for i in input_list:<br> if i%2 == 0:<br> count += 1<br> return count<br><br>def main():<br> randomList = generate_random_list()<br> print(randomList)<br> print(count_even_numbers(randomList))<br><br>if __name__ == "__main__":<br> main()<br>```<br>When I try to run this, I get the following message:<br><br>line 8, in count_even_numbers<br> if i%2 == 0:<br>TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'<br><br>**UPDATE:** I made a mistake in the title, so the real error message is there in the last code block. Sorry about that. <br><br>**SOLVED:** I forgot to make the function return values, so they returned None by default. However, I'm still going to post this thread for future reference.

Comments (6) 9813 👁️