ImportError: cannot import name ‘clock‘ from ‘time‘ (unknown location),解决办法
在采用蒙特卡罗方法求解π值时,出现上述报错,这是因为time.clock()在3.3版本后已移除,可使用**perf_counter()或者process_time()**代替。
代替后:
from time import process_time as timer
修改后:
from random import random from math import sqrt from time import process_time as timer DARTS = 1000 hits = 0.0 timer() for i in range(1, DARTS+1): x, y = random(), random() dist = sqrt(x ** 2 + y ** 2) if dist <= 1.0: hits = hits + 1 pi1 = 4 * (hits/DARTS) print("Pi值是{}.".format(pi1))