Q值。 )1 + 1/2! + 1/3! +¼! + ...... .....
以下程序的输出始终为1.0。我是一个蟒蛇初学者。请告诉我有什么问题?请建议任何其他方法,以使该计划更好。
请不要使用内置功能。</ strong>我想手动执行此操作。
n=int(raw_input("Enter number of terms : "))
d=0 #to sum all terms, initial value is 0
while n>0:
j=n #we start by taking factorial of the last term
s=1 #to multiply to find factorial
while j>0:
s=s*j #number gets multiplied to s and stored in s
j=j-1 #number is decreased until it reaches 0
n=n-1 #we will keep finding factorial until the very first term, i.e. 1 is reached
x=float(1/s) #to find reciprocal of the factorial we just found in floating point
d=d+x #adding to final sum
print "Sum of 1+1/2!+1/3!+1/4!+.... is ",d #printing
答案 0 :(得分:1)
你需要在你的师的某个地方使用浮动。我只是让你&#34; s&#34;一个漂浮物。
n=int(raw_input("Enter number of terms : "))
d=0 #to sum all terms, initial value is 0
while n>0:
j=n #we start by taking factorial of the last term
s=float(1) #to multiply to find factorial
while j>0:
s=s*j #number gets multiplied to s and stored in s
j=j-1 #number is decreased until it reaches 0
n=n-1 #we will keep finding factorial until the very first term, i.e. 1 is reached
x=1/s #to find reciprocal of the factorial we just found in floating point
d=d+x #adding to final sum
print "Sum of 1+1/2!+1/3!+1/4!+.... is ",d #printing