Exercise
Contents
Exercise¶
%matplotlib inline
import matplotlib.pyplot as plt
from math import sin, cos, tan, pi, acos
from myturtle import Turtle
Coding exercise¶
- Explain what the following program does. 
t = Turtle()
t.forward(1)
t.left(120)
t.forward(1)
t.left(120)
t.forward(1)
t.left(120)
- run the program yourself and see if you are right. 
- Write a program that draws a square. 
- Execute the example and explain what happens. 
t = Turtle()
t.forward(3.2)
if t.x > 3:
    t.left(180)
    t.forward(t.x - 3)
    t.left(180)
t.left(180)
t.forward(1)    
- Change the threshold from 3 to 2. 
- How could you improve this code to make it more maintainable? 
- Execute the following code for different numerical values in the second line and explain what happens. 
t = Turtle()
t.forward(1)
if 1 <= t.x < 2:
    t.left(45)
elif 2 <= t.x < 3:
    t.left(90)
elif 3 <= t.x < 4:
    t.left(135)
else:
    t.left(180)
t.forward(1)
- What happens, if the second condition is changed to - 1 <= t.x < 3and the initial step size is set to 1?
- Execute the following code and explain what happens. 
t = Turtle()
while t.x <= 5:
    t.forward(1)
- Insert a - t.left(90)before the loop and execute the code. What happens? (you can interrupt the program by hitting- <ctrl>-cor- itwice)
- Explain what risk arise when a - whileloop is used.
- Execute the code below and explain what it does in colloquial words. 
t = Turtle()
while t.x <= 5:
    t.forward(0.1)
    if t.y > 2:
        break
    if t.x > 3:
        continue
    t.left(1)
- In which area will the cursor stay? 
- What is the difference between the following programs? 
t = Turtle()
while t.x < 5:
    t.forward(1)
and
t = Turtle()
while True:
    t.forward(1)
    if t.x < 5:
        break
- Rewrite this code, such that the - rangefunction is used in the loop header.
for i in [0, 1, 2, 3]:
    print(i)
- Write a program with - Turtle, which draws a circle (approximately).
- Write a program, which draws a equilateral triangle, rectangle, penta- and hexagon - one after anonther. The number of edges, the angles and side length are defined in the lists - N,- alphaand- abelow. Try to use the function- zip()!
N = range(3, 7)
alpha = [360 / n for n in N]
a = [sin(pi / n) for n in N]
Bonus Exercise¶
- Write a program, which causes Turtle to move in a circle with its’ center at (0, 1.35) and a radius of 3. 
- When Turtle hits the edge of the circle, it should be reflected physically correct. Angle of incidence is equal to angle of reflection. 
- Turtle should travel a distance of 500. 
The cell below already give you a head start.
Note that you do have the trigonometric function (sin, cos, tan, acos) and the constant pi from the math package available.
Hint: Until you are sure everything works as it should, start with a distance of 15.
O = (0, 1.35)       # center of the circle
r = 3               # radius of the circle
dl = .1             # distrance travelled at each iteration
dmax = 500          # maximum distance travelled
N = int(dmax // dl) # number of iterations
# draw circle
plt.gca().add_patch(plt.Circle(O, r));
plt.axis('equal');
t = Turtle()
# ...
