码迷,mamicode.com
首页 > 编程语言 > 详细

第四周(1):利用Python计算π的值,并显示进度条

时间:2020-03-24 23:33:17      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:利用   python   圆周率   ret   info   number   print   __call__   call   

用Python计算圆周率pi并用进度条提示进度

一 计算公式:

   技术图片

二 实现代码

(1)

 

import math
from tqdm import tqdm
import time
total,s,n,t=0.0,1,1.0,1.0
while(math.fabs(t)>=1e-6):
    total+=t
    n+=2 
    s=-s
    t=s/n
k=total*4
print("π值是{:.10f}  运行时间为{:.4f}秒".format(k,time.clock()))
for i in tqdm(range(101)):
    print("\r{:3}%".format(i),end="")
    time.sleep(0.1)

(2)

import time
import math
 
 
class Index(object):
    def __init__(self, number=50, decimal=2):
        self.decimal = decimal
        self.number = number
        self.a = 100/number   
 
    def __call__(self, now, total):
        percentage = self.percentage_number(now, total)
        well_num = int(percentage / self.a)
        progress_bar_num = self.progress_bar(well_num)
        result = "\r%s %s" % (progress_bar_num, percentage)
        return result
 
    def percentage_number(self, now, total):
        return round(now / total * 100, self.decimal)
 
    def progress_bar(self, num):
        well_num = "#" * num
        space_num = " " * (self.number - num)
        return ‘[%s%s]‘ % (well_num, space_num)
 
 
 
index = Index()


total,s,n,t=0.0,1,1.0,1.0
while(math.fabs(t)>=1e-6):
    total+=t
    n+=2 
    s=-s
    t=s/n
k=total*4
 
start = 371
for i in range(start + 1):
    print(index(i, start), end=‘‘)
    time.sleep(0.01)
    
    
print("\n π值是{:.10f}".format(k))

(3)

 

import time
import math
total,s,n,t=0.0,1,1.0,1.0
while(math.fabs(t)>=1e-6):
    total+=t
    n+=2 
    s=-s
    t=s/n
k=total*4
scale=50
print("".center(scale//2,"-"))
start = time.perf_counter()
for i in range(scale+1):
    a="*"*i
    b="."*(scale-i)
    c=(i/scale)*100
    d=time.perf_counter() - start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,d),end=‘‘)
    time.sleep(0.1)
print("\n π值是{:.10f}".format(k))

 

  

 

 

 

第四周(1):利用Python计算π的值,并显示进度条

标签:利用   python   圆周率   ret   info   number   print   __call__   call   

原文地址:https://www.cnblogs.com/linjiaxin59/p/12562697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!