Print pi to a number of decimal places

前端 未结 7 2480
感情败类
感情败类 2020-12-19 08:56

One of the challenges on w3resources is to print pi to \'n\' decimal places. Here is my code:

from math import pi

fraser = str(pi)

length_of_pi = []

numbe         


        
7条回答
  •  悲&欢浪女
    2020-12-19 09:36

    This is what I did, really elementary but works (max 15 decimal places):

    pi = 22/7
    while True:
    
        n = int(input('Please enter how many decimals you want to print: '))
    
        if n<=15:
            print('The output with {} decimal places is: '.format(n))
            x = str(pi)
            print(x[0:n+2])
            break
        else:
            print('Please enter a number between 0 and 15')
    

提交回复
热议问题