第三次作业-有进度条圆周率计算

有进度条圆周率计算

一,圆周率计算方法

1.公式法

 2.蒙特卡洛法

 撒点方法

 取一个正圆和一个正方形的1/4形成一个单位方形

 单位方形中有1/4个圆

 圆的面积和单位方形之比即为圆周率的相关数据

 然后进行撒点

 

二,设计进度条

1.直接打印

 1 import time
 2  
 3 #demo1
 4 def process_bar(percent, start_str='', end_str='', total_length=0):
 5     bar = ''.join(["33[31m%s33[0m"%'   '] * int(percent * total_length)) + ''
 6     bar = '
' + start_str + bar.ljust(total_length) + ' {:0>4.1f}%|'.format(percent*100) + end_str
 7     print(bar, end='', flush=True)
 8  
 9  
10 for i in range(101):
11     time.sleep(0.1)
12     end_str = '100%'
13     process_bar(i/100, start_str='', end_str=end_str, total_length=15)
14  
15 #demo2
16 # for i in range(0, 101, 2):
17 #   time.sleep(0.1)
18 #   num = i // 2
19 #   if i == 100:
20 #     process = "
[%3s%%]: |%-50s|
" % (i, '|' * num)
21 #   else:
22 #     process = "
[%3s%%]: |%-50s|" % (i, '|' * num)
23 #   print(process, end='', flush=True)

2.tqdm库

Tqdm 是一个快速,可扩展的Python进度条,可以在 Python 长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator)

使用样例:

1 from time import sleep
2 from tqdm import tqdm
3  
4 for i in tqdm(range(20)):
5     sleep(0.5)

3.progressbar

安装:

pip3 install progressbar2

使用样例:

1 import time
2 import progressbar
3 
4 number_of_entry = 77
5 with progressbar.ProgressBar(max_value=number_of_entry) as bar:
6     for i in range(number_of_entry):
7         time.sleep(0.1)
8         bar.update(i)

三,有进度条圆周率计算

代码

 1 import math
 2 import time
 3 scale=10
 4 print("执行开始")
 5 t=time.process_time()
 6 for i in range(scale+1):
 7     a,b='**'*i,'..'*(scale-i)
 8     c=(i/scale)*100
 9     π=4*(4*math.atan(1/5)-math.atan(1/239))
10     print("%{:3}[{}->{}]".format(a,b,c))
11     time.sleep(0.1)
12 print(π)
13 print("{:.2f}s".format(t))
14 print("执行结束")

效果:

 

  

 

原文地址:https://www.cnblogs.com/tantan0914/p/12601660.html