import turtle import random from math import sqrt from time import perf_counter DARTS=100000 # 抛洒点数 hits=0.0 start= perf_counter() turtle.hideturtle() for i in range(1,DARTS+1): x=random.uniform(0,200) y=random.uniform(0,200) dist=sqrt(x**2+y**2) if(dist<=200.0): hits=hits+1 turtle.penup() turtle.goto(x,y) turtle.dot(3,"red") if(i%10000==0): print("当前进度为:%",i/1000) else: turtle.penup() turtle.goto(x,y) turtle.dot(3,"blue") if(i%10000==0): print("当前进度为:%",i/1000) pi=4*(hits/DARTS) end = perf_counter() print("pi 值是{}".format(pi)) print("运行时间是:{:.5f}s".format(end-start)) turtle.done