Last active
July 19, 2021 12:14
-
-
Save minte9/ff4cfc6a032e398f265c071a65bccb57 to your computer and use it in GitHub Desktop.
Python / Language / Circle - https://www.minte9.com/python/language-loops-1250
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
""" | |
Write a function called circle that takes a turtle, t, and radius, r, | |
as parameters and that draws an approximate circle | |
by calling polygon with an appropriate length and number of sides. | |
Test your function with a range of values of r. | |
Hint: figure out the circumference of the circle | |
and make sure that length * n = circumference. | |
import turtle | |
def poligon(t, length, n): | |
for i in range(n): | |
t.fd(length) | |
t.lt(360/n) | |
bob = turtle.Turtle() | |
poligon(bob, 20, 10) | |
turtle.mainloop() """ | |
# SOLUTION | |
import turtle | |
import math | |
def poligon(t, length, n): | |
for i in range(n): | |
t.fd(length) | |
t.lt(360/n) | |
def circle (t, r): | |
circumference = 2 * math.pi * r | |
n = 360 # slow | |
n = 36 # fast | |
length = circumference/ n | |
poligon(t, length, n) | |
bob = turtle.Turtle() | |
circle(bob, 50) | |
circle(bob, 100) | |
turtle.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment