r/PythonLearning • u/Leviathans_ • 1d ago
Help Request help for a programing assignment
I need to write a program that will calculate the sum of the first n of 1 + 1/2 + 1/4 + ... = lim (n -> ∞) Σ 1/2^k =2 starting at k=0 to n.
Teacher said we could use Σ 1/2^k = 2 - 1/2^(n-1) starting at k=0 to n-1
We have to write it using a loop.
I went back through my notes and figured I needed to use a «for» loop.
My teacher starts all of his programs with def, but when I try to look up more infos online I struggle to find others who are using it.
So, here's what I've tried
def estimate_serie (nb_term) :
sum = 0
for term in range (1, nb_term + 1) :
sum=sum + 1/(2 ** term)
return sum
print (estimate_serie(5))
print (estimate_serie(8))
print (estimate_serie(12))it gave me
0.5
0.5
0.5
Btw I am learning this in french so I might not have translated all of it correctly. I've also only been learning for a month, so if my mistakes seem dumb that might explain why.
•
u/Sea-Ad7805 1d ago edited 1d ago
Run program above by clicking Memory Graph Web Debugger and see its state change step by step.
It gives output:
Oh IC, you indented (put white-space in front of)
return sumtoo much like in this code which gives the output you describe.Indentation is very important in python, as with this indentation the
return sumis part of the loop, so the loop will run only once and then return. The Memory Graph Web Debugger visualization makes it clear what going wrong, use it for debugging.4
u/Due-Mortgage-6854 1d ago
Your
return summust be outside the FOR Loop and yourfor term in range(1, nb_term + 1)must start from zero giving :for term in range(nb_term + 1)2
u/Naive_Programmer_232 1d ago
hmm when I run your code on my side I get
your code has a small error. consider this equation
if
k=0to account for the zeroth term, then the sum should be:but in your code you have this
this happens because the range becomes
range(1,1)and the loop will not execute, so in this casesum=0which is incorrect, it should be 1.to get around this edge case you can do one of two things to account for zeroth term:
range(1,nb_term+1)but setsum=1.0range(nb_term)and initializesum=0.0both of these will calculate exactly nb_term amount of terms.
now one thing to note, conventionally, you want to be careful with using built-in function names as variables. the reason for this is python will overwrite the reference to the original function it is pointing to. so by doing
sum=0, sincesumis a built-in function, it is no longer representing that function, instead it's value now is0. in this case it isn't as big as an issue sincesumis overwritten locally, it won't change the global definition,anyhow, I ran your function then tested it against two other rewrites i made for the first 50 terms and a random set of 50 terms and it works:
anyway good luck! hope this helps on the code side!!
extra: math learning about why
f3solution works (what your teacher provided):we know that for k+1 terms this is true,
and in
f3we saw how by limiting to k-1 we got the following for k terms:In order to verify however that the first statement is actually true, we can use a proof technique known as mathematical induction:
2
u/Dazzling_Music_2411 1d ago
I ran it (correcting for your zero mistake) and it runs fine. I think you must have a mistake in your indentations.
When you post code, can you please format it properly, otherwise no one can tell what is going on.
1
u/Repulsive-Song-1594 23h ago
Man i have the same problem but simpler and i cannot understand how to use function and lists inside a loop to calculate something if you found a way to learn that through a video hit me up
0
u/bra1n_gym 1d ago
should just be
sum = 0
for i in range(n):
sum += (1/2)**n
print(sum)