圆周率pi的运输,这报错是因为我电脑缺少time的库吗?

img

pi.py

from random import random
from math import sqrt
from time import clock
DARTS = 1200
hits = 0
clock()
for i in range(1,DARTS):
x, y = random(), random()
dist = sqrt(x2 + y2)
if dist <= 1.0:
hits = hits + 1
pi = 4 * (hits/DARTS)
print("Pi的值是 %s" % pi)
print("程序运行时间是 %-5.5

img


ss" % clock())

3.3版本之后应该已经移出了,可使用perf_counter()或者process_time()代替。

time.clock 方法从 Python 3.3 开始已经被废弃了, 可以使用 time.perf_counter() or time.process_time() 作为替代
time — Time access and conversions — Python 3.7.13 documentation

你的python版本是多少?某个版本后,time没有clock的方法了。
如果需要计时,可以用time.time() 或者time.perf_counter().