zoukankan      html  css  js  c++  java
  • 用python进行有进度条的圆周率计算

    一、安装tqdm函数库

    tqdm是一个强大的终端进度条工具,我利用pip获取tqdm函数库。

    1、打开运行,输入“cmd”

    2、2:输入pip install   你要安装的库(如 pip install tqdm)

    此处我已经装好了。

     二、编写代码

    我用书上的代码

    from random import random
    from math import sqrt
    from time import *
    from tqdm import tqdm
    DARTS=10000000
    hits=0.0
    clock()
    for i in range(1,DARTS+1):
        x,y=random(),random()
        dist=sqrt(x**2+y**2)
        if dist <=1.0:
            hits=hits+1
    pi=4*(hits/DARTS)
    for i in tqdm(range(10)):
        print("
    {:3}%".format(i/10*100),end="") #这里的i/10*100指每10%显示一次
    sleep((clock())/100)#用执行程序的总时间来算出进度条间隔的时间
    print("pi的值{}.".format(pi))
    print("运行时间:{:.5f}s".format(clock()))
    三、执行效果

    
    
  • 相关阅读:
    u Calculate e
    Elevator
    骑士走棋盘
    Number Sequence
    老鼠走迷宫
    Let the Balloon Rise
    A+B Problem II
    Three-Color Flag
    Noldbach problem
    Almost Prime
  • 原文地址:https://www.cnblogs.com/loverboy88/p/10563607.html
Copyright © 2011-2022 走看看