Python Functions: Using Built-in Functions and Creating Functions 1/8

My Code:

``````          661, 1246, 1000, 7429, 1376, 8121, 647, 1280,
3993, 4881, 9500, 6701, 1199, 6251, 4432, 37]
sum_manual = 0
for row in a_list:
sum_manual += a_list

print(sum_manual)
print(sum(a_list)```

What I expected to happen:
``````

i don’t know the expected answer.

``````
What actually happened:
``````

Output
Traceback (most recent call last):
File “”, line 9
print(sum(a_list)
^
SyntaxError: unexpected EOF while parsing

``<!--Enter other relevant details below: --> Need some help understanding the question, the syntax, and the question. Sorry, I'm slow when it comes to coding and understanding the question.``

Hi @kunal.shukla285
Add a “)” closing parenthesis to your print statement.
print(sum(a_alist))

It looks like you are comparing summing the values using a for loop vs summing the values using sum( ) function. You should get the same answer either way.

sum_manual = 0
for a_value in a_list:
sum_manual += a_value

• You created a variable sum_manual and set it equal to 0.
sum_manual = 0

• You created a for loop to loop through the a_list [ ] list .
for a_value in a_list

• Lets go through the first loop where a_list is equal to the first value of 661
sum_manual += a_value
sum_manual = sum_manual + a_value
sum_manual = 0 because it is empty
sum_manual = sum_manual + 661
sum_manual = 661

• Let’s go another loop in the for loop
a_list is now equal to the 2nd value in the a_list which is 1246
sum_manual was equal to 661 from first loop
sum_manual = 661 + 1246
now sum_manual is equal to 1907

• This would continue until you reach the end of a_list
all the values in a_list would be added in sum_manual and you would end up with the sum of a_list in sum_manual

• The value from print(sum(a_list)) will be the same number as print(sum_manual)

3 Likes

Thank you! How did you know to use a_value? that’s the part that confused me.

You can name the variable anything ( within allowed syntax ). You usually want to select a variable name that is meaningful so you know what it is being used for.

for a_value in a_list:
value_from_a_list = a_value

you can also use:

for a_number in a_list:
value_from_a_list = a_number

You just want to be sure the two names match so the value can be stored in a variable:

• a_value is stored in the temporary variable in the for loop statement
for a_value in a_list:
the number 661 is stored in a_value
so a_value = 661 in the 1st for loop

• now you want to add a_value to sum_manual
sum_manual +=a_value
you want to make sure you use a_value so you can access the value 661

2 Likes