Last active
September 30, 2020 07:15
-
-
Save shinysu/9149e7bb1374fabf15080c5c951d01ec to your computer and use it in GitHub Desktop.
draw bot program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 would you like to draw?") | |
user_input = input() | |
if user_input == 'circle': | |
draw_circle(100) | |
elif user_input == 'square': | |
draw_shape(4, 100) | |
print("Thank You!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 would you like to draw?") | |
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) | |
print("Thank You!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 would you like to draw?") | |
user_input = input() | |
if user_input == 'circle': | |
draw_circle(100) | |
elif user_input in polygon_sides: | |
sides = polygon_sides[user_input] | |
draw_shape(sides, 100) | |
print("Thank You!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
use else condition to handle all other cases | |
lower() - convert a string to lower case | |
''' | |
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 would you like to draw?") | |
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, I don't know how to draw that shape") | |
print("Thank You!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
using while loop to execute the loop continuosly | |
clear() - to clear the screen | |
''' | |
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 would you like to draw?") | |
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, I don't know how to draw that shape") | |
print("Thank You!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment