Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Summing a list of numbers
#1
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 <module>
    x += num
NameError: name 'x' is not defined
>>> 
I would appreciate any advice or help.

Thank you
Reply
#2
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
#3
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
#4
(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
#5
Thanks for pointing that out. I'm still quite new myself, haha.
Reply
#6
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
#7
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
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,443 Jan-05-2024, 08:30 PM
Last Post: sgrey
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,014 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Summing up set elements(HH:MM:SS) tester_V 4 1,104 Dec-22-2022, 10:03 AM
Last Post: perfringo
  List of random numbers astral_travel 17 2,533 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,430 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Divide a number by numbers in a list. Wallen 7 7,926 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  producing numbers out of a list bouraque7878 10 3,624 Nov-12-2021, 09:13 PM
Last Post: jefsummers
  Summing up rows and columns plumberpy 3 2,220 Aug-18-2021, 05:46 AM
Last Post: naughtyCat
  How to change odd to even numbers in the list? plumberpy 8 3,621 Aug-08-2021, 11:07 AM
Last Post: plumberpy

Forum Jump:

User Panel Messages

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