问题描述
我们必须计算 Pi。
我的代码
import sys
import math
for s in sys.stdin:
m = math.pi
t = str(m).split(".")
tok = ("").join(t)
x = tok[0] + "."
r = t[1]
y = int(s)
print(x + r[0:y])
这会计算 PI,但我的教授希望它是
1 = 3.1
2 = 3.14
3 = 3.142
4 = 3.1416
5 = 3.1415926536
我的是 3.1
3.14
3.141
3.1415
3.1415926535
我如何使它与我的教授相似?
解决方法
使用类似的东西
array[count]=len(str(pi).split(".")[1])
然后很容易按照你想要的方式打印