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
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))
+ 5
What if you separate into these two lines?
final = (1-r**n)/(1-r)
print("The number is:", final)
+ 2
Thanks 👍👍👍👍❤️