用海龟图形创建乘法表——编码不正确

2024-09-30 14:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试创建一个显示乘法表的程序,就像这样。 [![在此处输入图像说明][1]][1]

我在编码方面遇到了很多问题。当我像现在这样运行程序时,它看起来是这样的: [![在此处输入图像说明][2]][2]

下面是我的代码:

import turtle

turtle.hideturtle()
turtle.penup()

turtle.write(("   Multiplication Table"), font =("Arial",20,"bold"))

for dash in range(-13, 250, 5):
    turtle.goto(dash, -40)
    turtle.write(("  -  "), font =("Arial", 10, "bold"))

j = 1 

for topFactor in range( 15, 240, 28):

    turtle.goto( topFactor, -30)

    turtle.write( str( j )+ "   |",font=("Arial",10,"bold"))

    j +=1

l = 1 

for rightFactor in range( 50, 240, 22):

    turtle.goto( -10, -rightFactor)

    turtle.write( str( l )+ "   |",font=("Arial",10,"bold"))

    l +=1 

    for topFactor in range(15, 240, 28):

        turtle.goto( topFactor, -50)

        turtle.write( str(l * j), font=("Arial",10,"bold"))

turtle.done()

我会很感激你的帮助。在


Tags: in图像程序forrangewritefontturtle
1条回答
网友
1楼 · 发布于 2024-09-30 14:20:28

这一行阻止数字填充整个表:

turtle.goto( topFactor, -50)

它应该是:

^{pr2}$

另外,您对l * j的计算是关闭的,因为j在代码中此时不会随列而递增。这里有一个潜在的返工:

import turtle

turtle.hideturtle()

turtle.penup()

turtle.write(("   Multiplication Table"), font = ("Arial", 20," bold"))

for dash in range(-13, 250, 5):
    turtle.goto(dash, -40)
    turtle.write(("  -  "), font = ("Arial", 10, "bold"))

for j, topFactor in enumerate(range(15, 240, 28)):

    turtle.goto(topFactor, -30)

    turtle.write(str(j + 1) + "   |", font = ("Arial", 10, "bold"))

for l, rightFactor in enumerate(range(50, 240, 22)):

    turtle.goto(-10, -rightFactor)

    turtle.write(str(l + 1) + "   |", font = ("Arial", 10, "bold")) 

    for j, topFactor in enumerate(range(15, 240, 28)):

        turtle.goto(topFactor, -rightFactor)

        turtle.write(str((l + 1) * (j + 1)), font = ("Arial", 10, "bold"))

turtle.done()

相关问题 更多 >