首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于可能的逻辑错误,除了最后一行之外,绘图进行得很顺利。

由于可能的逻辑错误,除了最后一行之外,绘图进行得很顺利。
EN

Stack Overflow用户
提问于 2022-06-06 04:23:28
回答 1查看 31关注 0票数 1

所以我想画一排对称的圆圈。请稍等一下,因为代码很长。

代码语言:javascript
复制
# DRAWING THE DIAGRAM
pen = turtle.Turtle() # use turtle pen
pen.speed(-1) # speed of pen
pen.hideturtle() # hide the turtle
radius = 10 # radius of the symmetrical tubes
big_radius = 1.10*radius*max(columns) # radius of the shell of the heat exchanger wrt number of max columns
pen.circle(big_radius) # draw the shell
increment = 10
pen.pu()
#  CALCULATE MOST TUBES FIRST AKA MAX COLUMNS (ALSO TELLS US WHICH ROW IT IS)
for i in range(int(max(columns)/2)): # cross section is symmetrical hence columns/2 (drawing on the positive x-axis)
    pen.goto(increment, big_radius-radius)
    pen.pd()
    pen.circle(radius)
    pen.pu()
    pen.forward(radius*2)
    increment += 20

pen.pu()
radius *= -1 # this is to achieve symmetry on the negative axis
increment = -10
for i in range(int(max(columns)/2)): # as above, but now drawing on negative x-axis to maintain symmetry
    pen.goto(increment, big_radius - radius)
    pen.pd()
    pen.circle(radius)
    pen.pu()
    pen.forward(radius * 2)
    pen.pd()
    increment -= 20
pen.pu()

radius *= -1 # initialize to positive axis
increment = 10 # initialize to positive axis

middle_row = max(dict, key=dict.get)
# 1st row after the middle row (+ve y-axis, x-axis)
for i in range(1,2):
    pen.goto(increment,(big_radius + radius))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.forward(radius * 2)
        increment +=20

**# subsequent rows after the first row (+ve x-axis) [MAIN CULPRIT]
for i in range(2,middle_row):
    increment = 10
    pen.goto(increment,big_radius + i*(radius+5))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.forward(radius * 2)
        increment += 20**

# for 1st row after middle row (-ve x-axis)
for i in range(1,2):
    increment = -10
    pen.goto(increment,(big_radius + radius))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.backward(radius * 2)
        increment -= 20

**# subsequent rows after the first row (-ve x-axis) [MAIN CULPRIT]
for i in range(2,middle_row):
    increment = -10
    pen.goto(increment,big_radius + i*(radius+5))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.backward(radius * 2)
        increment -= 20**

radius *= -1 # initialize to negative y-axis

# 1st row after the middle row (-ve y-axis, -ve x-axis)
for i in range(1,2):
    increment = -10
    pen.goto(increment,(big_radius + radius))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.forward(radius * 2)
        increment += 20

# subsequent rows after the first row (-ve x-axis)
for i in range(2,middle_row):
    increment = -10
    pen.goto(increment,big_radius + i*(radius-5))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.forward(radius * 2)
        increment -= 20

# 1st row after the middle row (+ve x-axis)
for i in range(1,2):
    increment = 10
    pen.goto(increment,(big_radius + radius))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.backward(radius * 2)
        increment += 20

**# subsequent rows after the first row (+ve x-axis) [MAIN CULPRIT]
for i in range(2,middle_row):
    increment = 10
    pen.goto(increment,big_radius + i*(radius-5))
    for j in range(int(dict[middle_row-i]/2)):
        pen.pd()
        pen.circle(radius)
        pen.pu()
        pen.backward(radius * 2)
        increment += 20**

我用主要的罪魁祸首突出了可能促成这一效果的代码。除了最后的上行和下行之外,一切都正常工作,除了代码中的逻辑错误之外,我似乎找不出问题。

上述图形的图像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-06 18:39:12

在我看来,如果我们将18个for循环减少到2个,这段代码将更容易调试:

代码语言:javascript
复制
from turtle import Screen, Pen

dictionary = {2: 2, 3: 4, 4: 6, 5: 8}  # mapping of row # -> row width
columns = dictionary.values()

RADIUS = 10 # RADIUS of the symmetrical tubes
BIG_RADIUS = 1.10 * RADIUS * max(columns)

screen = Screen()

pen = Pen()
pen.hideturtle()
pen.speed('fastest')

pen.penup()
pen.sety(-BIG_RADIUS)
pen.pendown()
pen.circle(BIG_RADIUS)
pen.penup()

max_rows = max(dictionary)
pen.sety(max_rows * RADIUS)

for row in range(2, 2 * max_rows - 1):
    columns = dictionary[row if row < max_rows else 2 * max_rows - row]

    pen.setx(-RADIUS * (columns - 1))

    for _ in range(columns):
        pen.pendown()
        pen.circle(RADIUS)
        pen.penup()
        pen.setx(pen.xcor() + RADIUS*2)

    pen.sety(pen.ycor() - RADIUS*2)

screen.exitonclick()

你没有提供dict,所以我不得不猜测--根据需要调整。我修改了代码,使图像保持在窗口的中心,从而简化了数学运算。下面是几个编程说明:

代码语言:javascript
复制
 pen.speed(-1)

对于speed(),这不是一个有效的参数值。此外,不要(重新)为变量使用内置类型的名称:

代码语言:javascript
复制
 dict[middle_row-i]

在本例中使用dict以外的其他东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72513049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档