Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

python - 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.

question from:https://stackoverflow.com/questions/65890934/i-need-to-write-a-program-that-reads-integers-from-stdin-and-for-each-integer-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...