I got this
02-06-2016, 01:28 AM
I'm in a intro to Python class and our assignment is to estimate Pi but I keep getting an error I don't understand. The error is
NameError: name 'win' is not defined
The reason I don't understand the error is that I've defined win in the
def create_win(w,h) function.
My code is:
from graphics import *
from math import *
import random
def generateXY(n):
xlist = []
ylist = []
for i in range(n):
xlist.append(random.uniform(-1,1))
ylist.append(random.uniform(-1,1))
return xlist, ylist
def create_window(w, h):
win = GraphWin("Monte Carlo",w,h)
win.setCoords(-1,-1,1,1)
return win
# create points list
def create_points(xlist, ylist):
points = []
for i in range(len(xlist)):
points.append((xlist[i], ylist[i]))
return points
# draw a circle with given radius
def draw_circle(w, radius):
center = Point(0,0)
c= Circle(center, radius)
c.draw(win)
return c
def draw_points(win, xylist):
for point in xylist:
p = Point (*point)
p.draw(win)
def distance_from_center(x,y):
return math.sqrt((x ** 2 + y ** 2))
# estimate pi using Monte Carlo simulation
def simulate_pi(win, xylist):
draw_points(win, xylist)
total_points = len(xylist)
points_inside = 0
for p in xylist:
if (distance_from_center(p[0],p[1]) <= 1):
points_inside += 1
percentError = points_inside/total_points
if percentError == .85:
return (8.22536130248883)
estimate_pi = 4 * (points_inside/total_points)
print(estimate_pi, percentError)
return percentError
def main():
xlist,ylist = generateXY(1000)
xylist = create_points(xlist, ylist)
win = create_window(500,500)
draw_circle(win, 1)
error = simulate_pi(win, xylist)
win.getMouse()
win.close
if __name__ == '__main__':
main()
Any help is much appreciated.
Thanks a bunch,
~I Got This
NameError: name 'win' is not defined
The reason I don't understand the error is that I've defined win in the
def create_win(w,h) function.
My code is:
from graphics import *
from math import *
import random
def generateXY(n):
xlist = []
ylist = []
for i in range(n):
xlist.append(random.uniform(-1,1))
ylist.append(random.uniform(-1,1))
return xlist, ylist
def create_window(w, h):
win = GraphWin("Monte Carlo",w,h)
win.setCoords(-1,-1,1,1)
return win
# create points list
def create_points(xlist, ylist):
points = []
for i in range(len(xlist)):
points.append((xlist[i], ylist[i]))
return points
# draw a circle with given radius
def draw_circle(w, radius):
center = Point(0,0)
c= Circle(center, radius)
c.draw(win)
return c
def draw_points(win, xylist):
for point in xylist:
p = Point (*point)
p.draw(win)
def distance_from_center(x,y):
return math.sqrt((x ** 2 + y ** 2))
# estimate pi using Monte Carlo simulation
def simulate_pi(win, xylist):
draw_points(win, xylist)
total_points = len(xylist)
points_inside = 0
for p in xylist:
if (distance_from_center(p[0],p[1]) <= 1):
points_inside += 1
percentError = points_inside/total_points
if percentError == .85:
return (8.22536130248883)
estimate_pi = 4 * (points_inside/total_points)
print(estimate_pi, percentError)
return percentError
def main():
xlist,ylist = generateXY(1000)
xylist = create_points(xlist, ylist)
win = create_window(500,500)
draw_circle(win, 1)
error = simulate_pi(win, xylist)
win.getMouse()
win.close
if __name__ == '__main__':
main()
Any help is much appreciated.
Thanks a bunch,
~I Got This