user2441453
user2441453

Reputation: 11

New to programming, learning python. Trying to get this program to work

First, a link to the "problem":

http://interactivepython.org/courselib/static/thinkcspy/Labs/montepi.html

I'm doing good up until getting a counter set up. I feel confident in doing the rest once I've gotten that figured out.

import turtle
import math
import random

fred = turtle.Turtle()
fred.shape("circle")

wn = turtle.Screen()
wn.setworldcoordinates(-1,-1,1,1)

def main():

    count = 0

    def ifDist():
        if fred.distance(0,0) > 1:
            fred.color("blue")
        else:
            fred.color("red")
            counter()   

    def counter():
        count = count + 1
        return count

    def darts():
        numdarts = 2
        for i in range(numdarts):
            randx = random.random()
            randy = random.random()

            x = randx
            y = randy

            fred.penup()
            fred.goto(randx, randy)
            ifDist()
            fred.stamp()
            fred.goto(randx, -randy)
            ifDist()
            fred.stamp()
            fred.goto(-randx, randy)
            ifDist()
            fred.stamp()
            fred.goto(-randx, -randy)
            ifDist()
            fred.stamp()

    darts()

    print(count)

main()


wn.exitonclick()

It keeps printing 0 for the counter. I've been trying at this for a couple days (at least this code doesn't give an error message...) and I'm sure it's a simple fix, but I just don't know what it would be. Any assistance would really be appreciated.

EDIT: included counter() in the else statement, as I had previously done when tinkering with it. It now calls the counter, but I do get an error as well:

Traceback (most recent call last): File "C:\Users\USER\Google Drive\School\PYTHON\5_16_piEstimator.py", line 53, in main() File "C:\Users\USER\Google Drive\School\PYTHON\5_16_piEstimator.py", line 49, in main darts() File "C:\Users\USER\Google Drive\School\PYTHON\5_16_piEstimator.py", line 37, in darts ifDist() File "C:\Users\USER\Google Drive\School\PYTHON\5_16_piEstimator.py", line 20, in ifDist counter() File "C:\Users\USER\Google Drive\School\PYTHON\5_16_piEstimator.py", line 23, in counter count = count + 1 UnboundLocalError: local variable 'count' referenced before assignment

Upvotes: 1

Views: 323

Answers (2)

John La Rooy
John La Rooy

Reputation: 304137

Apart from not calling your counter() function, this won't work anyway.

As @Perkins mentioned in the comments, you can't modify a reference outside of your scope. You can't increment count because int are immutable in Python. count = count + 1 is creating a new int object and discarding the old one. The new instance needs to be bound to the count variable

Assuming Python3, you can say count is "nonlocal"

def counter():
    nonlocal count
    count = count + 1
    return count

which will tell Python it's ok to change the binding of count in main instead of trying to use a local (to counter) variable called count

Upvotes: 3

eidsonator
eidsonator

Reputation: 1325

Your counter function is never called, hence the count never increments.

Upvotes: 1

Related Questions