0

How can I add all of the values of 'n'?

list = [] calc = 1 while int(calc) < 51: list.append(calc) calc = int(calc) + 1 for n in list: n = 1/n**2 print (n). Right now that will basically print 50 values starting with 1 then 0.25, 0.1111, 0.0625 and so on.... I just want to know, how can I take those values and add them together.

5th Feb 2020, 7:55 AM
Elmi Jimenez
Elmi Jimenez - avatar
8 Respuestas
+ 2
Okay, that's a lot simpler then. You don't need the list or the inner loop. calc = 1 x = 0 while calc<51: x += 1/calc**2 calc = calc + 1 print (x) This would be your code trimmed down. We prepare x=0, and then, we directly add the part calculation, as you described, to x with each iteration. Try to play this code through in your mind and see how it does what you prescribed. (This can be reduced to a single line of code, but that is something you should look forward to learning later. :))
5th Feb 2020, 9:14 PM
HonFu
HonFu - avatar
+ 3
You are printing the result x with every loop. If you remove the indentation and print x only once, is the result how you expect? (In case indentation is still tripping you up, you might wanna read (not run) this: https://code.sololearn.com/cT5BRIbkia21/?ref=app )
5th Feb 2020, 6:07 PM
HonFu
HonFu - avatar
+ 2
Indentation is absolutely important in Python, because it governs which statements belongs to a block of code (like a loop) and which don't. (In many other languages this is done using curly brackets. What algorithm is this exactly? I don't know what sort of calculation is supposed to lead to your expected result.
5th Feb 2020, 8:10 PM
HonFu
HonFu - avatar
+ 1
One way is to create a variable before the loop. set it to 0. Then, with every run of the loop, you add the value you want to add, either x = x + yourcalculation or simply x += yourcalculation
5th Feb 2020, 8:56 AM
HonFu
HonFu - avatar
+ 1
Uff, a bit new to python, didn't think indentation would play such a big role. ok bro, so now am getting the single digit that I wanted, but for some reason is way off. output: 78.38256407636845 and according to my calculations it can't go higher than 2. am expecting the answer to be: ~1.5024 this is the actual out put of "n": 1.0 0.25 0.1111111111111111 0.0625 0.04 0.027777777777777776 0.02040816326530612 0.015625 0.012345679012345678 0.01 0.008264462809917356 ... and what i want the program to do is this with the data: 1.0 + 0.25 + 0.1111111111111111 + 0.0625 + 0.04 + 0.027777777777777776 + 0.020408163 + ... + 0.0004 = the answer
5th Feb 2020, 7:57 PM
Elmi Jimenez
Elmi Jimenez - avatar
+ 1
I am basically trying to calculate a Riemann sum. from 1 - 50 (1/(x^2)) the calculation is suppose to go like this: 1/(1^2)+ 1/(2^2) + 1/(3^2) + 1/ (3^4) + ... + 1/(50^2) = answer as of now I was able to get the output for all individual numbers: 1/1= 1, 1/4=0.25, 1/9 = 0.111111, .... , 1/(50^2)= o.ooo4 now what i am trying to do is add all of the numbers to get a single digit. Basically this: 1.0 + 0.25 + 0.1111111111111111 + ... + 0.0004 = ~ 1.5024 The only output that I want is a float near this: ~1.5024 How can i add the list of these 50 numbers?
5th Feb 2020, 9:02 PM
Elmi Jimenez
Elmi Jimenez - avatar
0
hey Honfu... this is what i did know: list = [] calc = 1 x = 0 while int(calc) < 51: list.append(calc) calc = int(calc) + 1 for n in list: n = 1/n**2 x += n print (x) however, answers still came up: 1.0 2.25 3.611111111111111 5.034722222222221 6.498333333333332 .... the output should be the sum of all the previous values, combine to a single output. but i def think we are getting some were, i'll keep playing around with our idea and see if I can figure it out. Basically what am trying to do is take the **Riemann sum from 1 to 50 of 1/x^2**
5th Feb 2020, 5:34 PM
Elmi Jimenez
Elmi Jimenez - avatar
0
you are a god ma dude, thanks man, keep me posted with your codes, I learned a ton from the indent project that you had.
6th Feb 2020, 1:24 AM
Elmi Jimenez
Elmi Jimenez - avatar