[英]I need to write a program that reads integers from stdin and, for each integer read, prints pi to that number of decimal places
Doing some lab questions and I came across this one, been stuck on it for ages not sure what to do from here, heres my code:做了一些实验问题,我遇到了这个问题,被困了很久不知道从这里做什么,这是我的代码:
import sys
import math
for number in sys.stdin:
number = number.strip()
pi = str(math.pi)
print(format(pi, number))
My code is just printing pi, also i forgot to mention Im still a beginner so we haven't come across every technique.我的代码只是打印 pi,我也忘了提到我还是个初学者,所以我们还没有遇到每一种技术。
you could use round instead format:您可以使用圆形代替格式:
import sys
import math
for n in sys.stdin:
number = int(n.strip())
pi = math.pi
print(round(pi, number))
Use indexing.使用索引。
import sys
import math
for n in sys.stdin:
number = int(n.strip())
pi = str(math.pi)
print(pi[:number+2])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.