为了账号安全,请及时绑定邮箱和手机立即绑定

如何每行发布一个字符

如何每行发布一个字符

繁花如伊 2022-06-02 12:01:57
任务是我们必须编写一个程序来显示 pi 的前 10 位数字。每个数字应每行显示一个,末尾没有句号。我理解整个导入语句,但我在 for 循环中有很多错误。
查看完整描述

2 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

嗨 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'


查看完整回答
反对 回复 2022-06-02
?
慕的地10843

TA贡献1785条经验 获得超8个赞

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


from math import pi


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


for i in range(10):

    print strPiDigits[i]



查看完整回答
反对 回复 2022-06-02
  • 2 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号