[英]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 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. 它为计数器保持打印0。 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. 编辑:在else语句中包含counter(),就像我以前在修补它时所做的那样。 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 回溯(最近一次调用最后一次):文件“C:\\ Users \\ USER \\ Google Drive \\ School \\ PYTHON \\ 5_16_piEstimator.py”,第53行,在main()文件“C:\\ Users \\ USER \\ Google Drive \\ School \\ PYTHON \\ 5_16_piEstimator.py“,第49行,在主飞镖()文件”C:\\ Users \\ USER \\ Google Drive \\ School \\ PYTHON \\ 5_16_piEstimator.py“第37行,用飞镖ifDist()文件”C:\\ Users \\ USER \\ Google Drive \\ School \\ PYTHON \\ 5_16_piEstimator.py“,第20行,ifDist计数器()文件”C:\\ Users \\ USER \\ Google Drive \\ School \\ PYTHON \\ 5_16_piEstimator.py“,第23行,计数器数= count + 1 UnboundLocalError:赋值前引用的局部变量'count'
Apart from not calling your counter()
function, this won't work anyway. 除了不调用你的counter()
函数之外,这无论如何都行不通。
As @Perkins mentioned in the comments, you can't modify a reference outside of your scope. 正如@Perkins在评论中提到的那样,您无法修改范围之外的引用。 You can't increment count
because int
are immutable in Python. 你不能增加count
因为int
在Python中是不可变的。 count = count + 1
is creating a new int
object and discarding the old one. count = count + 1
正在创建一个新的int
对象并丢弃旧的对象。 The new instance needs to be bound to the count variable 新实例需要绑定到count变量
Assuming Python3, you can say count is "nonlocal" 假设Python3,你可以说count是“非本地的”
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 这将告诉Python可以在main
更改count的绑定,而不是尝试使用名为count的本地(计数器)变量
永远不会调用您的计数器函数,因此计数永远不会增加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.