Skip to content

Instantly share code, notes, and snippets.

@shinysu
Last active July 26, 2021 06:03
Show Gist options
  • Save shinysu/f12076361d8499f756f6baea2bcefc9f to your computer and use it in GitHub Desktop.
Save shinysu/f12076361d8499f756f6baea2bcefc9f to your computer and use it in GitHub Desktop.
Day3 - python turtle
'''
a program to draw a circle and a square
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
draw_circle(100)
draw_shape(4, 100)
'''
draw_bot_v1.py
- uses input() function to get the user input
- use if, elif to check for a condition and perform an action
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
print("What shape do you want?")
user_input = input()
if user_input == 'circle':
draw_circle(100)
elif user_input == 'square':
draw_shape(4, 100)
'''
using multiple elif statements for conditional check
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
print("What shape do you want?")
user_input = input()
if user_input == 'circle':
draw_circle(100)
elif user_input == 'square':
draw_shape(4, 100)
elif user_input == 'pentagon':
draw_shape(5, 100)
elif user_input == 'hexagon':
draw_shape(6, 100)
elif user_input == 'septagon':
draw_shape(7, 100)
elif user_input == 'octagon':
draw_shape(8, 100)
'''
using dictionary to store the number of sides for the polygon type
dictionary store data as key-value pair. Here the name of the polygon is the key and the number of sides is stored as the value
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
polygon_sides = {'triangle': 3, 'square': 4, 'pentagon': 5, 'hexagon': 6, 'septagon': 7, 'octagon': 8}
print("What shape do you want?")
user_input = input()
if user_input == 'circle':
draw_circle(100)
elif user_input in polygon_sides:
draw_shape(polygon_sides[user_input], 100)
'''
use else condition to handle all other cases
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
polygon_sides = {'triangle': 3, 'square': 4, 'pentagon': 5, 'hexagon': 6, 'septagon': 7, 'octagon': 8}
print("What shape do you want?")
user_input = input().lower()
if user_input == 'circle':
draw_circle(100)
elif user_input in polygon_sides:
draw_shape(polygon_sides[user_input], 100)
else:
print("Sorry, can't draw that shape")
'''
using while loop to continue drawing different shapes
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
polygon_sides = {'triangle': 3, 'square': 4, 'pentagon': 5, 'hexagon': 6, 'septagon': 7, 'octagon': 8}
while True:
print("What shape do you want?")
user_input = input().lower()
t.clear()
if user_input == 'circle':
draw_circle(100)
elif user_input in polygon_sides:
draw_shape(polygon_sides[user_input], 100)
elif user_input == 'exit':
break
else:
print("Sorry, can't draw that shape")
print("Thank You!")
'''
Get user input for length of the shape
int(input()) - used to convert user input to integer
'''
import turtle
t = turtle.Turtle()
def draw_circle(radius):
t.circle(radius)
def draw_shape(sides, length):
angle = 360 / sides
for i in range(sides):
t.forward(length)
t.left(angle)
polygon_sides = {'triangle': 3, 'square': 4, 'pentagon': 5, 'hexagon': 6, 'septagon': 7, 'octagon': 8}
while True:
print("What shape do you want?")
user_input = input().lower()
t.clear()
if user_input == 'circle':
radius = int(input("Enter the radius:"))
draw_circle(radius)
elif user_input in polygon_sides:
length = int(input("Enter the length:"))
draw_shape(polygon_sides[user_input], length)
elif user_input == 'exit':
break
else:
print("Sorry, can't draw that shape")
print("Thank You!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment