Function That Computes Sum of Squares of Numbers in List
<p>I'm trying to write a function sum_of_squares(xs) that computes the sum of the squares of the numbers in the list xs. For example, sum_of_squares([2, 3, 4]) should return 4+9+16 which is 29:</p>

<p>Here's what I tried:</p>

<pre><code>import random

xs = []

#create three random numbers between 0 and 50

for i in range(3):
    xs.append(random.randint(0,50))

def sum_of_squares(xs):

#square the numbers in the list

    squared = i ** i

#add together the squared numbers

    sum_of_squares = squared + squared + squared

    return sum_of_squares

print (sum_of_squares(xs))
</code></pre>

<p>Right now this always prints</p>

<pre><code>12
</code></pre>

<p>Because it's taking i as in the number of integers in the list as opposed to the value of the integer. How do I say "multiply the value by the value of the integer" for as many integers are in the list to get the squared values? </p>

<p>Asking that question led me to try this:</p>

<pre><code>import random

xs = []

#create three random numbers between 0 and 50

for i in range(3):
    xs.append(random.randint(0,50))

def sum_of_squares(xs):

#square the numbers in the list

    for i in (xs):
        squared = i ** i

#add together the squared numbers

        sum_of_squares = squared + squared + squared

    return sum_of_squares

print (sum_of_squares(xs))
</code></pre>

<p>But it doesn't seem to be squaring the values of the integers correctly - I'm not sure what it's doing. See this screenshot <a href="https://i.stack.imgur.com/E7Uv5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E7Uv5.png" alt="See Screenshot"></a> of a <a href="http://www.pythontutor.com/visualize.html#mode=edit" rel="nofollow noreferrer">Visualize Python</a> walkthrough. </p>