Pages

Tuesday, January 31, 2017

Python: TurtleWorld and Polygons

In this blog post, I will be continuing on with the TurleWorld and seeing if I can make different shapes and objects using my previous knowledge and guidance from the book. For example, in the first exercise, it wants me to simplify my coding by adding a “for” statement. “A for statement is sometimes called a loop because of the flow of execution runs through the body and then loops back to the top” (Downey 39). In this case, I used the “for” statement to simplify and make my square coding not as repetitive. 



In this example, I had the line of code that I used in the last post to repeat 4 times, instead of me typing it out 4 times. In the next exercise in Think Python. It wants me to fiddle around with the angle of the turns that the turtle can make. In this case, by making the polygon a function, I can change around what I can input in its parameters to change the shape that I make with the Turtle. In the example that Think Python gives me, the first line of code defines the function “polygon” to have the variables, t, n, and length. Then in the function of the polygon, there is a variable called angle, which calculates the exterior angle of the polygon. Then there is a loop statement again that moves the turtle (t) in a certain length, and then turns the turtle left in a certain angle. The last line of the code executes the function “polygon” with 7 sides with each side length being 70 (Downey 39).





During the next examples in Think Python there was also a challenge to program it so that the turtle draws a circle. While I tried my best to figure it out I eventually ran out of time and gave up to be able to post on the blog on time.

Question: When have you given up on something because it was too difficult?


Downey, Allen B. Think Python: How to Think like a Computer Scientist. Sebastopol: O'Reilly Media, 2015. Print

0 comments:

Post a Comment