[Python] Calculate pi With MonteCarlo

import random
import math

m=int(raw_input("Please input the Number of Times:"))
#Then Output n=10~10^m
n=1
for j in xrange(m):
  n=n*10
  Total=0
  for i in xrange(n):
        x=random.random()
        y=random.random()
        if math.sqrt(x*x+y*y)<1.0:
            Total+=1
    # print x,y,Total
  MonteCarlo_pi=4.0*Total/n
  print "Esitmating pi with",n,"iterations",MonteCarlo_pi
  print "Value of math.pi is",math.pi
  print "Error is ",abs(math.pi-MonteCarlo_pi)/math.pi
  print


关于Python几点感言:

1.灵活度

2.缩进强迫症、

   对于我这种不缩进会死的人没什么影响。某些人死都不知道怎么死的。

  不过显然没有缩进的话Python在基础应用时程序体之间会非常混乱

3.先定义后使用与不定义自动定义使用的问题

4.大小写敏感

5.多指令同义

6.简单但功能实际非常复杂

原文地址:https://www.cnblogs.com/Catch-22/p/4116329.html