I am using a for loop to fill a list with 100 random numbers. I want to add those numbers once the list is complete and I cannot figure out how to do that. I am pretty sure it is something simple that I have just overlooked but it is driving me crazy that I can't get it to work.
Asked
Active
Viewed 36 times
-5
-
1Welcome to SO. Before asking questions, make sure that you've searched your question first. – S.B Sep 09 '22 at 18:04
2 Answers
-1
Use sum(list)
list = [random.randint(1,9) for i in range(100)]
print(sum(list))

walker
- 444
- 2
- 12
-
-
-
Yes it works but just not a good practice. Later lines may not work!. Just to mention I didn't downvote. – S.B Sep 09 '22 at 18:16
-
-1
Python has a built in sum
function
list_sum = sum(your_list)

unltd_J
- 476
- 7
- 14
-
Does this work if you are using a loop to fill the list? I have tried this and it isn't working. – 1tgraham Sep 09 '22 at 19:06
-
you do it outside of the loop after the loop is completed, not in. @1tgraham – unltd_J Sep 10 '22 at 11:33