我一直在考虑这个问题,无法解决。也许你可以帮我。问题是我的代码无法以Python编码语言输出1000位的pi。
这是我的代码:
def make_pi(): q, r, t, k, m, x = 1, 0, 1, 1, 3, 3 while True: if 4 * q + r - t < m * t: yield m q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x) else: q, r, t, k, m, x = (q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2) digits = make_pi() pi_list = [] my_array = [] for i in range(1000): my_array.append(str("hello, I'm an element in an array \n" )) big_string = "".join(my_array) print "here is a big string:\n %s" % big_string
我知道该代码可以修复,但是我不确定该修复什么。。。这句print话说的是一个很大的字符串,my_array.append(str("hello, im an element in an array \n))现在只是填充符。我知道所有代码都是如何工作的,但是就像我之前说的那样,我无法拿出它来发布该代码。
print
my_array.append(str("hello, im an element in an array \n))
运行这个
def make_pi(): q, r, t, k, m, x = 1, 0, 1, 1, 3, 3 for j in range(1000): if 4 * q + r - t < m * t: yield m q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x else: q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2 my_array = [] for i in make_pi(): my_array.append(str(i)) my_array = my_array[:1] + ['.'] + my_array[1:] big_string = "".join(my_array) print "here is a big string:\n %s" % big_string
并yield从此处了解有关运算符的信息: “ yield”关键字的作用是什么?
yield
答案是:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337