how to align text in python turtle with drawn polygon with n sides
<p>I am a beginner at python and am writing a simple program with python turtle, prompting the user to enter a side length of a polygon, and the program is supposed to draw the polygon and print the name of the person (me) under the polygon.</p>

<p>I have gotten the program to work, however I cannot seem to figure out how to get the text to print under the polygon, because the side length can be changed by the user, so the polygon sometimes can go out of view, depending how large the side length is imputed.</p>

<p>the program is supposed to look like this:
<a href="http://i.stack.imgur.com/on0Ka.png" rel="nofollow">end result</a> </p>

<p>However, mine looks sort of like this each time:
<a href="http://i.stack.imgur.com/g6SZy.png" rel="nofollow">My result</a></p>

<p>My code is as follows:</p>

<pre><code>import turtle

print('************************************************')
print('This program draws a randomly colored polygon')
print('with side lengths provided by the user.')
print('************************************************')

polygonSideLength = int(input('Enter length of polygon side: \n'))
numberOfSides = int(5 + (28 / 4))
turnAngle = 360 / numberOfSides

import random
randomColor = random.randint(0,5)
if randomColor == 0:
    fillcolor="red"
elif randomColor == 1:
    fillcolor="green"
elif randomColor == 2:
    fillcolor="blue"
elif randomColor == 3:
    fillcolor="cyan"
elif randomColor == 4:
    fillcolor="magenta"
elif randomColor == 5:
    fillcolor="yellow"
print('Length of polygon side =', polygonSideLength)
print('Number of polygon sides =', numberOfSides)
print('Turn angle at each vertex =', turnAngle)
print('Random fill color is', fillcolor)

turtle.begin_fill()
turtle.pen(pensize = 5, pencolor="black", fillcolor = fillcolor)

count = 0
while (count &lt; numberOfSides):
    turtle.forward(polygonSideLength)
    turtle.right(turnAngle)
    count = count + 1
turtle.end_fill()

turtle.setheading(270)
turtle.penup()
turtle.forward(65)
turtle.left(90)
turtle.forward(130)
turtle.pendown()
turtle.write("polygon drawn by: Name", align = "right", font=("Arial", 12, "normal"))
turtle.hideturtle()
turtle.done()
</code></pre>

<p>Can someone help me on how to change this code so that it works?</p>

<p>Let's try to go the simplest route to a solution.  First, let's get your polygon centered on the window.  We can do that by adding:</p>

<pre><code>turtle.backward(polygonSideLength / 2)
</code></pre>

<p>before the filled polygon drawing begins.  Next, let's get it into the upper half of the window instead of the lower half.  We can do that by changing:</p>

<pre><code>turtle.right(turnAngle)
</code></pre>

<p>to:</p>

<pre><code>turtle.left(turnAngle)
</code></pre>

<p>I.e. just draw it in the other direction.  Now that we have the polygon centertered horizontally and above the center line vertically, we just need to write centered text below the center line displaced by the height of the font (or two to make some space):</p>

<pre><code>fontSize = 18
...
turtle.goto(0, -fontSize * 2)

turtle.write("polygon drawn by: Name", align="center", font=("Arial", fontSize, "normal"))
</code></pre>

<p>Putting it all together, and making some simple style changes, we get:</p>

<pre><code>import turtle
import random

colors = ["red", "green", "blue", "cyan", "magenta", "yellow"]

print('************************************************')
print('This program draws a randomly colored polygon')
print('with side lengths provided by the user.')
print('************************************************')

polygonSideLength = int(input('Enter length of polygon side: '))
numberOfSides = 5 + (28 // 4)  # I assume this should be something more interesting...
turnAngle = 360 / numberOfSides
fontSize = 18

randomColor = random.randint(0, 5)
fillcolor = colors[randomColor]

print('Length of polygon side =', polygonSideLength)
print('Number of polygon sides =', numberOfSides)
print('Turn angle at each vertex =', turnAngle)
print('Random fill color is', fillcolor)

turtle.pen(pensize=5, pencolor="black", fillcolor=fillcolor)

turtle.backward(polygonSideLength / 2)

turtle.begin_fill()

for count in range(numberOfSides):
    turtle.forward(polygonSideLength)
    turtle.left(turnAngle)

turtle.end_fill()

turtle.penup()
turtle.setheading(270)

turtle.goto(0, -fontSize * 2)

turtle.write("polygon drawn by: Name", align="center", font=("Arial", fontSize, "normal"))
turtle.hideturtle()

turtle.done()
</code></pre>

<p><a href="http://i.stack.imgur.com/Zq4S2.png" rel="nofollow"><img src="http://i.stack.imgur.com/Zq4S2.png" alt="enter image description here"></a></p>

python turtle-graphics