Using Turtle module to display a 9 x 9 multiplication table


#1

Hi,
I am having trouble aligning the numbers. How can I fix this. I suspect that it might have to do with the size and spacing. I can sort of see the numbers taking up two spaces. Originally, I thought they would only occupy one space. I used grid paper to plan everything out and assumed every character would take up the one space. Apparently not. How would I fix this?

Thanks. Heres my code:

# (Turtle: display a multiplication table)

import turtle


turtle.penup()
turtle.goto(-20, 140)
turtle.pendown()
turtle.write("          Multiplication Table", font=("Arial", 20, "normal"))

turtle.penup()
turtle.goto(-40, 100)
turtle.pendown()
turtle.write("        1    2    3    4    5    6    7    8    9", font=("Arial", 20, "normal"))
turtle.penup()
turtle.goto(-40, 80)
turtle.pendown()
turtle.write("- - - - - - - - - - - - - - - - - - - - - - - - - - -", font=("Arial", 20, "normal"))

y = 60

for i in range(1, 9 + 1):
    turtle.penup()
    turtle.goto(-40, y)
    turtle.pendown()
    string = ""
    string = str(i) + " | "

    for j in range(1, 9 + 1):
        string = string + format(i * j, ">4d")

    turtle.write(string, font=("Arial", 20, "normal"))

    y -= 20



turtle.hideturtle()
turtle.done()

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.