Can some one help me fix my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can some one help me fix my code?

This is a Geometric sequence t1 calculator s1=float(input("The Geometric series's sum is:")) r=float(input("The common ratio:")) n=int(input("please enter the n:")) final=print("The number is:",(1-r**n)/(1-r)) #s1/final=t1 print("The t1 is",(s1/final)) #print t1

2nd Apr 2023, 7:45 PM
Andy birder
Andy birder - avatar
3 Answers
+ 6
Andy birder , final = print("The number is:", (1-r**n)/(1-r)) > this line above is assigning the *return value* of a print statement to the variable *final*, which is *None*. in the last line of the code a division is done like: (s1/final) the float input value in the variable *s1* is divided by *None* which creates a *TypeError*. > we can use the *walrus operator* (assignment expression): ... print("The number is:", res := (1-r**n)/(1-r)) print("The t1 is", (s1/res))
3rd Apr 2023, 2:22 PM
Lothar
Lothar - avatar
+ 5
What if you separate into these two lines? final = (1-r**n)/(1-r) print("The number is:", final)
2nd Apr 2023, 8:45 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
Thanks 👍👍👍👍❤️
3rd Apr 2023, 3:04 PM
Andy birder
Andy birder - avatar