关于python:1 + 1/2!

1 + 1 / 2! + 1/ 3! + ?! + …… Finding Sum

本问题已经有最佳答案,请猛点这里访问。

q + 1)/ 2。+ 1 / 3。+?!+回事… 以下程序的输出的是总是1。我是一个beginner Python。请告诉我什么是错的?请建议其他任何方法让这个程序更大。 不inbuilt当日,请。我想做这个manually。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    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


你需要在你部门的某个地方使用浮动。我只是让你成为一个漂浮物。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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