在python的IDLE中计算Π值的代码错误在哪?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python的IDLE中计算Π值的代码错误在哪?相关的知识,希望对你有一定的参考价值。

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(x**2+y**2)
if dist <=1.0:
hits = hits + 1
pi = 4*(hits/DARTS)
print("Pi的值是%s" % pi)
print("程序运行时间 %-5.5ss" % clock())
显示第三行有错误cannot import name 'clock' from 'time' (unknown location)

参考技术A python高版本的time模块里面没有.clock方法了,粗略的就time.time。
clock的功能替代方法就是另一个回答者说的那个
参考技术B 你那只是为了记录运行时间,可以使用perf_counter
from time import perf_counter

具体用法你搜下本回答被提问者采纳

python 另一个Pythonπ计算器

"""
Calculate digits of pi. This module can be run interactively with

    python pidigits.py

"""
__docformat__ = 'plaintext'

import sys
import math
from time import clock

from mpmath.libmp import bin_to_radix, numeral, pi_fixed

def display_fraction(digits, skip=0, colwidth=10, columns=5):
    perline = colwidth * columns
    printed = 0
    for linecount in range((len(digits)-skip) // (colwidth * columns)):
        line = digits[skip+linecount*perline:skip+(linecount+1)*perline]
        for i in range(columns):
            print line[i*colwidth : (i+1)*colwidth],
        print ":", (linecount+1)*perline
        if (linecount+1) % 10 == 0:
            print
        printed += colwidth*columns
    rem = (len(digits)-skip) % (colwidth * columns)
    if rem:
        buf = digits[-rem:]
        s = ""
        for i in range(columns):
            s += buf[:colwidth].ljust(colwidth+1, " ")
            buf = buf[colwidth:]
        print s + ":", printed + colwidth*columns

def calculateit(base, n, tofile):
    intpart = numeral(3, base)
    skip = 1
    if base <= 3:
        skip = 2

    prec = int(n*math.log(base,2))+10

    print "Step 1 of 2: calculating binary value..."
    t = clock()
    a = pi_fixed(prec, verbose=True, verbose_base=base)
    step1_time = clock() - t

    print "Step 2 of 2: converting to specified base..."
    t = clock()
    d = bin_to_radix(a, prec, base, n)
    d = numeral(d, base, n)
    step2_time = clock() - t

    print "\nWriting output...\n"

    if tofile:
        out_ = sys.stdout
        sys.stdout = tofile
    print "%i base-%i digits of pi:\n" % (n, base)
    print intpart, ".\n"

    display_fraction(d, skip, colwidth=10, columns=5)
    if tofile:
        sys.stdout = out_
    print "\nFinished in %f seconds (%f calc, %f convert)" % \
        ((step1_time + step2_time), step1_time, step2_time)

def interactive():
    print "Compute digits of pi with mpmath\n"
    base = input("Which base? (2-36, 10 for decimal) \n> ")
    digits = input("How many digits? (enter a big number, say, 10000)\n> ")
    tofile = raw_input("Output to file? (enter a filename, or just press " \
        "enter\nto print directly to the screen) \n> ")
    if tofile:
        tofile = open(tofile, "w")

    calculateit(base, digits, tofile)
    raw_input("\nPress enter to close this script.")

if __name__ == "__main__":
    interactive()

以上是关于在python的IDLE中计算Π值的代码错误在哪?的主要内容,如果未能解决你的问题,请参考以下文章

2πa在python表达式?

如何在Python上实现用文本进度条体现π的计算过程

Python多进程计算圆周率 Pi (π) 的值(ProcessPoolExecutor)

用Python进行有进度条的π计算

python怎么运行

python自带的idle有啥用