首页 > 解决方案 > 如何每行发布一个字符

问题描述

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

标签: python

解决方案


嗨 Ecuadorian_Programmer。如果将数字转换为字符串,则可以控制小数位的位置。例如:

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'

如果你想在单独的行中打印 10 位 pi,这可以正常工作:

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'


推荐阅读