Total sum value of list of floats

609 views Asked by At

I've been trying to solve this question. Can anybody correct me if I'm writing the code wrongly? Because I think my answer is a bit weird, but can' really tell where's lacking.

Question The list "random_numbers" which contains 100 random floats is given. Write a for-loop such that all floats in random_numbers are added up to a total sum value (accumulate to the variable "sum_value")

import pandas as pd
import numpy as np
np.random.seed(0) 

random_numbers = np.random.rand(100).tolist()
sum_value = 0

My answer in finding the total sum value

for s in range(0,len(random_numbers)):
    sum_value += random_numbers[s]
    print(sum_value)

The print(sum_value) gives;

0.5488135039273248
1.2640028702997443
1.8667662463713883
2.411649429368285
2.83530422870719
3.481198341773846
3.9187855530365385
4.810558553818618
5.7742213143196475
6.157662833145425
6.949387871228089
7.478282790980994
8.046327352074925
8.971923990367586
9.042960048565472
9.130089348267013
9.15030774570734
9.982927591255278
10.761084342205129
11.631096490451947
12.60971483268471
13.408873396901434
13.870352759154366
14.650881935440822
14.769156361309754
15.409077382637278
15.552430670046325
16.49709958709591
17.01894790884598
17.433609848836504
17.69816546094113
18.472399150375345
18.928549482591894
19.496983431460542
19.515773231896897
20.133408728972775
20.745504451695197
21.362438448569954
22.306186527084577
22.988006826188062
23.347514726761847
23.78454668056119
24.482177876488453
24.542403348117723
25.20917006356339
25.87980793318155
26.090190494255392
26.219116791910245
26.53454514283443
26.89825591377705
27.46845268419493
27.90705419765725
28.895428035716474
28.997472846464504
29.20634960255934
29.367659120444333
30.020767445909733
30.274059048449516
30.740369821305823
30.984795413307424
31.143764996952942
31.254140138117247
31.91046972758252
32.048652678931134
32.245235040611185
32.61396021127215
33.434953441120086
33.532054716913144
34.369999624411946
34.46609803230591
35.44255749731931
35.91120869896701
36.887969787157346
37.49281530690239
38.2320788863007
38.271266678555016
38.554073641131424
38.674270202344594
38.970410399866736
39.08913811882098
39.40712129821496
39.82138429272963
39.885531789078414
40.57800390844843
41.14460536265501
41.409994853594455
41.93324290706116
42.0271834178196
42.60312991337578
43.532426110951995
43.85099506340332
44.518405443367
44.65020330577139
45.366530509889955
45.65593660283716
45.83912796484427
46.425640899654354
46.44574844584185
47.274688475059214
47.27938395125176
1

There are 1 answers

2
Joe Ferndz On BEST ANSWER

While your response is correct, I recommend that you don't print the sum each time. Instead use the for loop until you have accumulated all the values. Then print the sum.

#you created 100 random numbers so you know the length already
#so you dont need to figure out the range. it will be 0 thru 100.

for s in range(100): 
    sum_value += random_numbers[s]

print(sum_value)

This will print the final total.

As @Cedric_Druck said, you can just do a simple sum(my_list) to get the sum.

In your case, your code will be:

sum_value = sum(random_numbers)

You can also use the following to code to generate the random number and sum it.

from random import random
for _ in range(100):
    r = random() #if this does not work, then r = random.random()
    sum_value += r
print ('the sum of 100 random floats is :', r)