如何每行输入一个字符

时间:2019-09-19 22:41:35

标签: python

分配是我们必须编写一个显示pi的前10位数字的程序。每行每个数字应显示一个,最后没有任何句号。我理解整个导入语句,但是for循环存在很多错误。

2 个答案:

答案 0 :(得分:1)

嗨,厄瓜多尔程序员。如果将数字转换为字符串,则可以控制小数的位置。例如:

import numpy as np # For take pi

a = str(np.pi) # We transform the number into a string   

print(a.replace('.','')) # We delete the point decimal separator) 
  

'3141592653589793'

如果要在pi的10位分隔行中打印,则效果很好:

import numpy as np # For take pi

a = str(np.pi).replace('.','') # We transform the number into a string without the point decimal separator

for i in range(10):
    print(a[i]) # Print all numbers in separated lines

并且,如果您只想输出小数(而不是前3个),则可以重新定义变量:

a=a.replace(a[0],'')

print(a) # Only decimals
  

'1415926558979'

答案 1 :(得分:0)

您只需要使用print方法,为pi中的每个数字调用它。考虑以下代码:

from math import pi

strPiDigits = str(pi).replace(".", "")

for i in range(10):
    print strPiDigits[i]
相关问题