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

huangapple go评论69阅读模式
英文:

RecursionError when drawing a concentrated circle with Python turtle: How to fix?

问题

You're experiencing a RecursionError in your Python code. The issue seems to be related to the recursive function a(). To address this error, you can try increasing the recursion depth limit or refactoring your code to avoid deep recursion. Here's the translated code snippet for your reference:

# 你遇到了一个Python代码中的RecursionError。问题似乎与递归函数a()有关。
# 为了解决这个错误,你可以尝试增加递归深度限制或重构你的代码以避免深度递归。

import turtle as t
import random as r

p1 = t.Pen()
# 参数
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()

Please note that while this code translation retains the structure and logic of your original code, it doesn't address the RecursionError directly. You may need to modify the recursion logic within the a() function to prevent it from reaching the maximum recursion depth.

英文:

How can I fix RecursionError: maximum recursion depth exceeded while calling a Python object error?

I want to draw a concentrated circle inside of trapesoid.In order to do it I used this codeblock below.

I check the condition a+b=c+d ,a b c d are the sides of trapesoid.

What can be the reason of this error?

Here I share my code with you:

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 &gt; 150 or x1 &lt; -150 and x2 &gt; 150 or x2 &lt; -150 and x3 &gt; 150 or x3 &lt; -150:
pass
else:
x1 = x1_solve()
x2 = x2_solve()
y2 = y2_solve()
x3 = x3_solve()
nums()
nums()
if x1 &gt; 0:
side_1 = ((x2 + x1/2)**2 +(y2)**2)**(1/2)
side_2 = ((x3 - x1/2)**2 +(y2)**2)**(1/2)
elif x1 &lt; 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()

I share my console error with you as well:

Traceback (most recent call last):
File &quot;C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py&quot;, line 52, in &lt;module&gt;
a()
File &quot;C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py&quot;, line 49, in a
a()
File &quot;C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py&quot;, line 49, in a
a()
File &quot;C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py&quot;, line 49, in a
a()
[Previous line repeated 992 more times]
File &quot;C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py&quot;, line 47, in a
x3 = x3_solve()
File &quot;C:\Users\azaz9\PycharmProjects\pythonProject1\zabit.py&quot;, line 18, in x3_solve
x3 = r.randrange(x2 + 1, x2 + abs(x1) - 21)
File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\random.py&quot;, line 352, in randrange
return istart + self._randbelow(width)
File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\random.py&quot;, line 245, in _randbelow_with_getrandbits
k = n.bit_length()  # don&#39;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

The problem can be in recursion.However I can not handle this error.

答案1

得分: 0

我看到一些问题。首先,如果随机选择的 x2x1 的值太接近,这段代码可能会出现 "ValueError: empty range" 错误:

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 关于浮点数的评论很中肯(+1),而不是像 a + b == c - d 这样说,我们可以使用 abs((a + b) - (c - d)) > 0.1

另一件需要注意的事情是,在调用 turtle 的 write() 方法时,不需要提高或降低画笔,它与画笔是独立的。你应该重新阅读关于 global 关键字的作用,因为你可能过度使用它了。在 nums 中的条件不依赖于 y2,所以没有理由重新计算它。

我建议在使其正常工作之前,仅使用正坐标,然后再进行泛化。

英文:

I see a number of issues. First, it appears that this code could fail with an "ValueError: empty range" error if the randomly chosen values for x2 and x1 are too close together:

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

E.g. if x2 is 10 and x1 is 0, then the call randrange(11, -11) fails. Both of your recursive functions could easily be while loops instead:

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

But that won't stop a runaway calculation. @jasonharper's comment about floating point is spot on (+1) and instead of say a + b == c - d, we'd say something like abs((a + b) - (c - d)) &gt; 0.1

Other things to note is you don't need to raise or lower the pen when you call turtle's write() method, it's independent of the pen. You should reread about what global does as you're over using it. The condition in nums doesn't depend on y2 so there's no reason to recompute it.

I'd approach this by using only positive coordinates until you have it working and then generalize.

答案2

得分: -1

似乎问题出在函数 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): # 这里是添加的条件
          a()
英文:

It seems that the problem is on the function a() that doesn't have a properly termination condition. You should add a condition for calling back the a() recursively. I post you an example here:

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()

huangapple
  • 本文由 发表于 2023年5月22日 20:15:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定