##### Summing a list of numbers
 Summing a list of numbers Oldman45 Silly Frenchman Posts: 33 Threads: 11 Joined: Jul 2020 Reputation: Jul-12-2020, 01:10 PM (This post was last modified: Jul-12-2020, 01:21 PM by Yoriz.) I am teaching myself Python and following a YouTube tutorial I input the following: ```xs = [12, 14, 10] for num in xs: x += num print(x)```This works in the tutorial and sums the list to 36, but on my Python I get the following error message: ```Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> = RESTART: C:/Users/John/Documents/John''s files/Work/Coding/Think Like a Computer Scientist/Programmes/loop_exercise2a.py Traceback (most recent call last): File "C:/Users/John/Documents/John''s files/Work/Coding/Think Like a Computer Scientist/Programmes/loop_exercise2a.py", line 4, in x += num NameError: name 'x' is not defined >>> ```I would appreciate any advice or help. Thank you Reply Posts: 1,620 Threads: 31 Joined: Sep 2016 Reputation: Jul-12-2020, 01:23 PM In the tutorial, they must have defined x to 0 ```x = 0 xs = [12, 14, 10] for num in xs: x += num print(x)`````Output:36`` Reply palladium Not Blown Up Yet Posts: 61 Threads: 14 Joined: Nov 2019 Reputation: Jul-12-2020, 01:25 PM You need to assign x to a value first. Try: ```xs = [12, 14, 10] x = 0 for num in xs: x += num print(x)```Or you can just use the sum function: ```xs = [12, 14, 10] x = sum(num for num in xs) print(x)``` Reply Posts: 1,620 Threads: 31 Joined: Sep 2016 Reputation: Jul-12-2020, 01:32 PM (Jul-12-2020, 01:25 PM)palladium Wrote: Or you can just use the sum function: ```xs = [12, 14, 10] x = sum(num for num in xs) print(x)``` xs is already a list `num for num in xs` is making a list from a list `x = sum(xs)` is all that's needed. Reply palladium Not Blown Up Yet Posts: 61 Threads: 14 Joined: Nov 2019 Reputation: Jul-12-2020, 02:08 PM Thanks for pointing that out. I'm still quite new myself, haha. Reply sridhar Programmer named Tim Posts: 16 Threads: 6 Joined: Apr 2020 Reputation: Jul-12-2020, 02:30 PM (This post was last modified: Jul-12-2020, 02:31 PM by sridhar.) You must declare x previously so that you can run without not defined error. Or you can try this simple sum function ```xs = [12, 14, 10] x = sum(xs) print(x)``` Reply Oldman45 Silly Frenchman Posts: 33 Threads: 11 Joined: Jul 2020 Reputation: Jul-12-2020, 05:30 PM Greetings Yoriz, Palladium and Sridhar Thank you all for the education - very useful. Yoriz, apologies for not complying with the rules. I have taken a copy of your link and next time I will comply. Sridhar I rated Yoriz and Palladium but the system said I had used my ratings up for the day when I tried to rate you so sorry. Thanks to you all again Reply

 Possibly Related Threads… Thread Author Replies Views Last Post adding numbers in a list Nickd12 2 321 Jan-15-2021, 12:46 PM Last Post: Serafim Grouping and summing of dataset jef 0 436 Oct-04-2020, 11:03 PM Last Post: jef Print max numbers in a list jimmoriarty 1 407 Sep-25-2020, 07:29 AM Last Post: DPaul importing a list of numbers into python script barrypyth 8 976 Aug-22-2020, 09:10 PM Last Post: barrypyth How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 6 5,075 Jul-01-2020, 03:53 PM Last Post: ndc85430 Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 678 Jun-11-2020, 08:31 PM Last Post: Drone4four Merge 2 dataframes but avoid double summing of rows? vinaysalian17 0 475 Jun-03-2020, 01:48 AM Last Post: vinaysalian17 Assign the sum of 2 consecutive numbers in a list to a varibale Fenaz 3 706 Apr-05-2020, 10:30 PM Last Post: bowlofred output a list of random numbers 'x' columns wide adityavpratap 4 776 Jan-13-2020, 05:32 PM Last Post: perfringo How do I name a list while generating numbers on the fly? Pleiades 6 1,034 Dec-03-2019, 05:47 PM Last Post: jefsummers

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020