<commit_before>---
layout: post
author: dpcolar
title: Implement the n-sided polygon
categories: post
---

The n-sided polygon is implemented using turtlehack.
<br>

```python

import turtle
import turtlehack
import random

# A function that draws an n-sided polygon
def n_sided_polygon(turtle, n, color="#FFFFFF", line_thickness=1):
	'''
	Draw an n-sided polygon
	input: turtle, n, line_length
	'''
	# for n times:
	# Draw a line, then turn 360/n degrees and draw another

	# set initial parameters
  	turtle.degrees()
	line_length=80
  	turtle.pensize(line_thickness)
  	turn_angle = (360/n)
  	i = 1

	# Draw each line segment and turn

  	while (i <= n):
		turtle.color(color)
    		turtle.pendown()
    		turtle.forward(line_length)
   		turtle.penup()
    		turtle.right(turn_angle)
    		i += 1

	return 0
 
## MAIN ##
# set initial parameters
n=random.randint(3,12)

# create the Turle instance
graphic = turtle.Turtle()
# Call the polygon code
n_sided_polygon(graphic, n, turtlehack.random_color(), random.randint(4,8))

# Close and exit
ignore = raw_input("hit any key to continue:")

```

[!http://www.unc.edu/~pcolar/n_sided_polygon.png N-sided Polygon]<commit_msg>Update 2013-10-07-assignment.md<commit_after>---
layout: post
author: dpcolar
title: Implement the n-sided polygon
categories: post
---

The n-sided polygon is implemented using turtlehack.
<br>

```python

import turtle
import turtlehack
import random

# A function that draws an n-sided polygon
def n_sided_polygon(turtle, n, color="#FFFFFF", line_thickness=1):
	'''
	Draw an n-sided polygon
	input: turtle, n, line_length
	'''
	# for n times:
	# Draw a line, then turn 360/n degrees and draw another

	# set initial parameters
  	turtle.degrees()
	line_length=80
  	turtle.pensize(line_thickness)
  	turn_angle = (360/n)
  	i = 1

	# Draw each line segment and turn

  	while (i <= n):
		turtle.color(color)
    		turtle.pendown()
    		turtle.forward(line_length)
   		turtle.penup()
    		turtle.right(turn_angle)
    		i += 1

	return 0
 
## MAIN ##
# set initial parameters
n=random.randint(3,12)

# create the Turle instance
graphic = turtle.Turtle()
# Call the polygon code
n_sided_polygon(graphic, n, turtlehack.random_color(), random.randint(4,8))

# Close and exit
ignore = raw_input("hit any key to continue:")

```

![http://www.unc.edu/~pcolar/n_sided_polygon.png N-sided Polygon]
{'fraction_non_alphanumeric': 0.1163575042158516, 'fraction_numerical': 0.01770657672849916, 'mean_word_length': 3.862704918032787, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6256705', 'n_tokens_mistral': 1007, 'n_tokens_neox': 846, 'n_words': 238}