Python: Summing Odd/Even Random Numbers Generates TypeError: 'int' object is not iterable
<p>Yes, this question has been asked, but I cannot seem to apply the answers to my problem. There are several parts to this problem, and this is the biggest obstacle I've hit.</p>

<p>I need to generate a random list of 10 numbers between 10 &amp; 90. From those random numbers, I need to sum the totals of both the even and odd numbers.</p>

<pre><code>def playlist():
    nums = []
    for nums in range(10):
        # Get random list of 10 numbers
        my_nums = random.randint(10, 90)
        print (my_nums,end=' ')
        even = []
        odd = []
        for x in my_nums:
            if x % 2 == 0:
                even.append[x]
                print(even)
            else:
                odd.append[x]
                print(odd)
</code></pre>

<p>When I run this, sometimes I get one or two numbers (usually the first two odd numbers), but mostly I get TypeError: 'int' object is not iterable. </p>

<p>Not gonna lie - my first language is PHP, not Python and that's becoming a huge problem for me :(</p>

<p>Any help is appreciated.</p>

<p>There are a few things you seem to have misunderstood:</p>

<ul>
<li><code>range(10)</code> will give you (something that looks like) this list <code>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</code>.

<ul>
<li>You can use it with a for-loop to do something 10 times</li>
</ul></li>
<li><code>random.randint(10, 90)</code> will give you a single random number between 10 and 90 (not a list)</li>
</ul>

<p>With this information we can change your script to:</p>

<pre><code>import random

even_sum = 0
odd_sum = 0
for number_of_turns in range(10):
    # Get a random number
    number_this_turn = random.randint(10, 90)
    print(number_this_turn,end=' ')
    if number_this_turn % 2 == 0:
        even_sum += number_this_turn
        print("Sum of even numbers so far:", even_sum)
    else:
        odd_sum += number_this_turn
        print("Sum of odd numbers so far:", odd_sum)

print("Final sum of even numbers:", even_sum)
print("Final sum of odd numbers:", odd_sum)
</code></pre>

<p>But we can do better. You will learn that in Python, you will want very often to define a list (or an iterable) with the terms you need then do something with every term. So we can change your script to:</p>

<pre><code>import random

even_sum = 0
odd_sum = 0

random_numbers = [random.randint(10, 90) for x in range(10)]

for number in random_numbers:
    print(number,end=' ')
    if number % 2 == 0:
        even_sum += number
        print("Sum of even numbers so far:", even_sum)
    else:
        odd_sum += number
        print("Sum of odd numbers so far:", odd_sum)

print("Final sum of even numbers:", even_sum)
print("Final sum of odd numbers:", odd_sum)
</code></pre>

<p><code>random_numbers = [random.randint(10, 90) for x in range(10)]</code> is using a <a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a> to generate a list of 10 random numbers. You can then do a for-loop on each of these numbers where you can add to the evens' sum or to the odds' sum.</p>

<p>You can even simplify it even further like in @Take_Care_ 's answer but I guess you have a lot more to learn before you reach this level.</p>

python random range typeerror iterable