Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Code Help
#1
Hey guys, I'm new to python but I do need it for a simulation that I must use for a school project. I have come across a simulation http://www.101computing.net/estimating-p...ns-needle/ that I need to use but I'm unable to complete Task 2. Could you help me out? The code from the simulation is as follows:
#Estimating Pi using Buffon's Needle # www.101compiting.net/estimating-pi-using-buffons-needle
import turtle
import random
import math

boardWidth = 40
needleLength = 30
numberOfNeedles = 10
    
myPen = turtle.Turtle()
myPen.hideturtle()
myPen.speed(0)

y=180
#Draw floor boards
for i in range(0,10):
  myPen.penup()
  myPen.goto(-200,y)
  myPen.pendown()
  myPen.goto(200,y)
  y-=boardWidth

#Draw Needles
myPen.color("#f442d1")
for needle in range(0,numberOfNeedles):
  x=random.randint(-180,180)
  y=random.randint(-180,180)
  angle=random.randint(0,360)
  myPen.penup()
  myPen.goto(x,y)
  myPen.setheading(angle)
  myPen.pendown()
  myPen.forward(needleLength)
  

print("L = " + str(needleLength))
print("N = " + str(numberOfNeedles))
print("W = " + str(boardWidth))
The task is : Adapt this Python script to automatically detect if a needle is crossing a line.
Your Python script should then count how many needles are crossing a line and use this to estimate a value of Pi.

Please. I really need some help.
Reply
#2
It looks like you haven't done anything except post the code you are supposed to adapt. We like to see some effort on your part before we help, otherwise we feel like we're just doing your work for you.

First make sure you understand the two non-Python problems here: How to you tell if two lines are crossing, and how do you use this method to estimate Pi.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020