简体   繁体   English

我需要编写一个从标准输入读取整数的程序,并且对于每个 integer 读取,将 pi 打印到小数位数

[英]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.

相关问题 如何编写打印出从 1 到输入数字的所有正 integer 值并且每对数字翻转的程序? - How to write the program that prints out all the positive integer values from 1 up to the input number and each pair of numbers is flipped? 将 pi 打印到小数位数 - Print pi to a number of decimal places 编写一个程序,读入一个字符串并打印它是否 - Write a program that reads in a string and prints whether it 我如何编写一个基本程序,要求输入一个数字,然后打印出从 1 到该数字的总和? - How do i write a basic program that asks for a number and then prints out the sum from one to the number? 如何编写程序打印出 1 和给定数字之间的所有正整数,并在范围的两端之间交替? - How to write the program that prints out all positive integers between 1 and given number , alternating between the two ends of the range? 在 Python 中查找最多 n 位小数的数字。 另外,python 中的 math.pi 可靠吗? - Finding a number up to n decimal places in Python. Also, is math.pi from python reliable? 编写一个程序,从用户那里接受一个正整数并打印该整数的前四个倍数。 使用while循环 - Write a program that accepts a positive integer from the user and prints the first four multiples of that integer. Use a while loop 编写一个程序,在一行代码中打印1到100(含)的整数 - Write a program that prints the integers from 1 to 100 (inclusive) in one line of code, however 我需要编写一个仅在两个数字为整数时才打印两个数字之和的代码,否则返回错误? - I need to write a code which prints the sum of two numbers only when they are integer otherwise returns error? 如何编写一个从文本文件读取的Python程序,并构建一个映射每个单词的字典 - how to write a Python program that reads from a text file, and builds a dictionary which maps each word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM