python - 如何每行发布一个字符
问题描述
任务是我们必须编写一个程序来显示 pi 的前 10 位数字。每个数字应每行显示一个,末尾没有句号。我理解整个导入语句,但我在 for 循环中有很多错误。
解决方案
嗨 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'
推荐阅读
- python - 使用迭代器制作字典列表覆盖列表中的一个键条目
- javascript - 如何将删除按钮添加到 HTML 列表中?
- python - 在类实例和类定义上调用类装饰器有什么区别?
- kubernetes - 如何“开箱即用”地在 Kubernetes 上部署气流?
- c# - 为什么每当我在 asp.net core 3.1 中使用数据库优先方法更新模型时,IdentityDbContext 会更改为 DbContext?
- amazon-web-services - 每当指标增加 x 倍时,是否可以使用自动缩放来缩放 x?
- python - Facebook Selenium 如何获得喜欢的次数
- flutter - 未处理的异常:未处理的错误“包:bloc/src/transition.dart”:断言失败:第 27 行 pos 16:“nextState!= null”:不正确
- c++ - 当控制台窗口处于非活动状态时,C++ 等待按键
- javascript - 模板中的 Angular Firebase 集成