Turtle Graphics window 没有响应
Turtle Graphics window not responding
我正在尝试将我之前制作的 Julia 集合生成器翻译成 Python 代码。但是,当代码为 运行 时,海龟图形 window 立即停止响应并且不绘制任何内容。我是不是做错了什么,或者我遗漏了什么?也许我要求在 1 帧内完成 python 太多。请解释导致这种情况发生的原因以及我该如何解决。谢谢!
import turtle
import time
y_set = []
map_output = 0
iterations = 0
#turtle.hideturtle()
#turtle.speed(1)
生成 y 值列表
def y_set (r):
global y_set
y_set = []
for n in range ((360*2)+1):
y_set.append(n)
创建颜色值
def color (i, n):
output = map(i, 2, 10000, 0, 2500)
if output < 0:
output = 0
if output > 0:
output = 255
迭代 x
def repeat (n, r, i):
global iterations
global x
global y
aa = 0
ba = 0
ab = 0
a = 0
b = 0
for j in range (n):
iterations += 1
aa = a * a
bb = b * b
ab = 2 * a * b
a = ((aa - bb) + float(r))
b = (ab + float(i))
if (ab + bb) > 4:
break
turtle.setx(100 * x)
turtle.sety(100 * y)
color(iterations, n)
turtle.pendown()
turtle.penup()
迭代 y
def Julia (s, r, i, d):
global iterations
global y_set
global x
global y
global a
global b
y_set(s)
while len(y_set) > 0:
y = y_set[0]/360
del y_set[0]
x = -1.5
for n in range (round((700/(float(r)+1))+1)):
a = x
b = y
iterations = 0
repeat(10**d, r, i)
x += ((1/240)*s)
用户输入
real = input('Real: ')
imag = input('Imaginary: ')
Julia (1, real, imag, 100)
turtle.done()
此代码存在太多问题,无法重点关注算法错误。当我尝试 运行 它时,我得到 TypeError: 'int' object is not iterable
。具体问题:
这里的 i
参数被传递了一个数字:
iterations += 1
...
color(iterations, n)
...
def color(i, n):
output = map(i, 2, 10000, 0, 2500)
但是 Python 的 map
函数(和 Julia 的)需要一个函数作为它的第一个参数:
map(func, *iterables)
并且它 returns 将 func
应用于 iterables
的结果列表,但您将结果视为标量值:
output = map(i, 2, 10000, 0, 2500)
if output < 0:
output = 0
if output > 0:
output = 255
color()
函数从不使用它的第二个参数,也从不使用 returns 任何东西!
这里的变量a
& b
被当作全局变量,设置但不使用,就好像准备被repeat()
:
使用一样
global a
global b
...
a = x
b = y
iterations = 0
repeat(10 ** d, r, i)
但是 repeat()
使用的 a
和 b
是初始化为零的局部变量:
a = 0
b = 0
您有一个同名的函数和全局变量y_set
!
你的全局变量失控了。
我正在尝试将我之前制作的 Julia 集合生成器翻译成 Python 代码。但是,当代码为 运行 时,海龟图形 window 立即停止响应并且不绘制任何内容。我是不是做错了什么,或者我遗漏了什么?也许我要求在 1 帧内完成 python 太多。请解释导致这种情况发生的原因以及我该如何解决。谢谢!
import turtle
import time
y_set = []
map_output = 0
iterations = 0
#turtle.hideturtle()
#turtle.speed(1)
生成 y 值列表
def y_set (r):
global y_set
y_set = []
for n in range ((360*2)+1):
y_set.append(n)
创建颜色值
def color (i, n):
output = map(i, 2, 10000, 0, 2500)
if output < 0:
output = 0
if output > 0:
output = 255
迭代 x
def repeat (n, r, i):
global iterations
global x
global y
aa = 0
ba = 0
ab = 0
a = 0
b = 0
for j in range (n):
iterations += 1
aa = a * a
bb = b * b
ab = 2 * a * b
a = ((aa - bb) + float(r))
b = (ab + float(i))
if (ab + bb) > 4:
break
turtle.setx(100 * x)
turtle.sety(100 * y)
color(iterations, n)
turtle.pendown()
turtle.penup()
迭代 y
def Julia (s, r, i, d):
global iterations
global y_set
global x
global y
global a
global b
y_set(s)
while len(y_set) > 0:
y = y_set[0]/360
del y_set[0]
x = -1.5
for n in range (round((700/(float(r)+1))+1)):
a = x
b = y
iterations = 0
repeat(10**d, r, i)
x += ((1/240)*s)
用户输入
real = input('Real: ')
imag = input('Imaginary: ')
Julia (1, real, imag, 100)
turtle.done()
此代码存在太多问题,无法重点关注算法错误。当我尝试 运行 它时,我得到 TypeError: 'int' object is not iterable
。具体问题:
这里的 i
参数被传递了一个数字:
iterations += 1
...
color(iterations, n)
...
def color(i, n):
output = map(i, 2, 10000, 0, 2500)
但是 Python 的 map
函数(和 Julia 的)需要一个函数作为它的第一个参数:
map(func, *iterables)
并且它 returns 将 func
应用于 iterables
的结果列表,但您将结果视为标量值:
output = map(i, 2, 10000, 0, 2500)
if output < 0:
output = 0
if output > 0:
output = 255
color()
函数从不使用它的第二个参数,也从不使用 returns 任何东西!
这里的变量a
& b
被当作全局变量,设置但不使用,就好像准备被repeat()
:
global a
global b
...
a = x
b = y
iterations = 0
repeat(10 ** d, r, i)
但是 repeat()
使用的 a
和 b
是初始化为零的局部变量:
a = 0
b = 0
您有一个同名的函数和全局变量y_set
!
你的全局变量失控了。