使用Python turtle绘制集中圆时的RecursionError:如何修复?

r7knjye2  于 2023-05-23  发布在  Python
关注(0)|答案(2)|浏览(206)

如何修复RecursionError:调用Python对象时超出最大递归深度错误?
我想在trapesoid里面画一个集中的圆。为了做到这一点,我使用了下面的代码块。
我检查了条件a+B=c+d,a,b,c,d是梯形的边。
这个错误的原因是什么?
我在这里与你分享我的代码:

import turtle as t
import random as r

p1 = t.Pen()
# parameters
p1.speed(1)

def x1_solve():
    x1 = r.randrange(-300, 300)
    return x1
def x2_solve():
    x2 = r.randrange(-300, 300)
    return x2
def y2_solve():
    y2 = r.randrange(0, 250)
    return y2
def x3_solve():
    x3 = r.randrange(x2 + 1, x2 + abs(x1) - 21)
    return x3

x1 = x1_solve()
x2 = x2_solve()
y2 = y2_solve()
x3 = x3_solve()
def nums():
    global x1,x2,x3,y2
    if x1 > 150 or x1 < -150 and x2 > 150 or x2 < -150 and x3 > 150 or x3 < -150:
        pass
    else:
        x1 = x1_solve()
        x2 = x2_solve()
        y2 = y2_solve()
        x3 = x3_solve()
        nums()
nums()
if x1 > 0:
    side_1 = ((x2 + x1/2)**2 +(y2)**2)**(1/2)
    side_2 = ((x3 - x1/2)**2 +(y2)**2)**(1/2)
elif x1 < 0:
    side_1 = ((x2 - x1/2)**2 +(y2)**2)**(1/2)
    side_2 = ((x3 - abs(x1/2))**2 +(y2)**2)**(1/2)
def a():
    global side_1,side_2,x3,x2,x1
    if side_1 + side_2 == x3 - x2 + abs(x1):
        pass
    else:
        x3 = x3_solve()
        print(x3)
        a()
print(side_1)
print(side_2)
a()
p1.goto(x=abs(x1/2),y=0)
p1.pendown()
p1.forward(-1 * abs(x1))
p1.goto(x=x2,y=y2)
p1.goto(x=x3, y=y2)
p1.goto(x=abs(x1)/2,y = 0)
p1.penup()
p1.goto(x=-20,y=-100)
p1.pendown()
p1.write(x3-x2)
p1.penup()
p1.goto(x=-20,y=-120)
p1.pendown()
p1.write(abs(x1))
t.mainloop()

我也和你分享我的控制台错误:

Traceback (most recent call last):
  File "C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py", line 52, in <module>
    a()
  File "C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py", line 49, in a
    a()
  File "C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py", line 49, in a
    a()
  File "C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py", line 49, in a
    a()
  [Previous line repeated 992 more times]
  File "C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py", line 47, in a
    x3 = x3_solve()
  File "C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py", line 18, in x3_solve
    x3 = r.randrange(x2 + 1, x2 + abs(x1) - 21)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\random.py", line 352, in randrange
    return istart + self._randbelow(width)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\random.py", line 245, in _randbelow_with_getrandbits
    k = n.bit_length()  # don't use (n-1) here because n can be 1
RecursionError: maximum recursion depth exceeded while calling a Python object

Process finished with exit code 1

这个问题可能是在递归。但是我不能处理这个错误。

rbpvctlc

rbpvctlc1#

我看到了一些问题。首先,这段代码可能会失败,并出现“ValueError:如果x2x1的随机选择值太接近,则会出现“空范围”错误:

randrange(x2 + 1, x2 + abs(x1) - 21)

例如,如果x2为10,x1为0,则调用randrange(11, -11)失败。你的两个递归函数都可以很容易地变成while循环:

def a():
    global x3

    while (side_1 + side_2) != (x3 - x2 + abs(x1)):
        x3 = x3_solve()

但这并不能阻止失控的计算。@jasonharper关于浮点的评论是spot on(+1),而不是说a + b == c - d,我们会说abs((a + b) - (c - d)) > 0.1
其他需要注意的是,当你调用turtle的write()方法时,你不需要升高或降低笔,它独立于笔。你应该重新阅读一下global的功能,因为你已经使用过了。nums中的条件不依赖于y2,因此没有理由重新计算它。
我只使用正坐标来处理这个问题,直到你让它工作,然后推广。

5ssjco0h

5ssjco0h2#

问题似乎出在函数a()上,它没有正确的终止条件。您应该添加一个递归回调a()的条件。我在这里给你一个例子:

def a():
    global side_1,side_2,x3,x2,x1
    if side_1 + side_2 == x3 - x2 + abs(x1):
        pass
    else:
        x3 = x3_solve()
        print(x3)
        if side_1 + side_2 != x3 - x2 + abs(x1): #here is the added condition
          a()

相关问题