{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This program will open a drawing window and draw a square in the window. Sometimes the window will not be visable at first. You might have to move the web browser window around to see the drawing region." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "import turtle\n", "dan = turtle.Turtle()\n", "dan.shape('turtle')\n", "\n", "for i in range(4):\n", " dan.forward(50)\n", " dan.right(90)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can find a full list of all the turtle functions supported in Python 3.6 here:\n", "https://docs.python.org/3.6/library/turtle.html\n", "\n", "Now lets make a new function to draw the side of a figure." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def side(): \n", " dan.forward(50)\n", " dan.right(90)\n", "\n", "for i in range(4):\n", " side()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we didn't have to put in the import or object initilaization. That was part of the state of the notebook.\n", "\n", "Now lets add some color and change the width of the drawing pen if the edge number is even or odd. The percent is the modulo function." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def side(edge):\n", " if edge % 2:\n", " dan.color('blue')\n", " dan.pensize(6)\n", " else:\n", " dan.color('red')\n", " dan.pensize(2)\n", " dan.forward(50)\n", " dan.right(90)\n", "\n", "for i in range(4):\n", " side(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's add some variables and change the number of sides. Here we will make a six sided hexigon by changing the angle to be 60 and the range to be 6." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "def side(edge, distance, angle):\n", " # this will be true for alternate edges\n", " if edge % 2:\n", " dan.color('blue')\n", " dan.pensize(6)\n", " else:\n", " dan.color('red')\n", " dan.pensize(2)\n", " dan.forward(distance)\n", " dan.right(angle)\n", "\n", "# draw a six sided hexagon\n", "dan.clear()\n", "distance = 80\n", "angle = 60\n", "for i in range(6):\n", " side(i, distance, angle)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }