# https://docs.python.org/2/library/turtle.html
# Important commands : fd lt lt rt
### DEMO 1 Assignments and Print Statement
a=10
b=23.5
print a+b
### DEMO 2 Compound Print Statement
a=10
b=23.5
print "a+b=",a+b
### DEMO 3 Calculating on the print line is not a good idea
a=10
b=23.5
c=a+b
print "a+b=",c
### DEMO 4 Python as a calculator
from math import *
a=10
b=23.5
print sin(pi/2)
### DEMO 5 Learning by drawing,
##5.1, Show heading
from turtle import *
clear()
home()
##5.2 Show move, heading after one move
from turtle import *
clear()
home()
stride=100
turn=90
fd(stride);rt(turn)
##5.3 Set of statements
from turtle import *
clear()
home()
stride=100
turn=90
fd(stride);rt(turn)
fd(stride/10);lt(turn)
fd(stride/2);rt(turn)
### Demo 6 Draw a triangle: Repeat version
from turtle import *
clear()
home()
stride=100
turn=120
fd(stride);rt(120)
fd(stride);rt(120)
fd(stride);rt(120)
### Demo 7 Draw a triangle: infinite loop version
from turtle import *
clear()
home()
stride=100
turn=120
steps=3
nstep=0
while nstep <= steps:
fd(stride);rt(120)
### DEMO 8 Fixing the infinite loop
from turtle import *
clear()
home()
stride=100
turn=120
steps=3
nstep=0
while nstep <= steps:
fd(stride);rt(120)
nstep=nstep+1
## 8.1 Print number of steps
from turtle import *
clear()
home()
stride=100
turn=120
steps=3
nstep=0
while nstep <= steps:
fd(stride);rt(120)
nstep=nstep+1
print "nstep=", nstep
## 8.2 What do we do when we are outside the loop
## Shift tab deindents
from turtle import *
clear()
home()
stride=100
turn=120
steps=3
nstep=0
while nstep <= steps:
fd(stride);rt(120)
nstep=nstep+1
print nstep
ht()
print "we are done"
### DEMO 9 turn=100, Guess what, change steps to 10, Q: Predict whether trajectory is closed
### and repeatative.
from turtle import *
clear()
home()
stride=100
turn=100
steps=3
nstep=0
while nstep <= steps:
fd(stride);rt(turn)
nstep=nstep+1
print nstep
ht()
print "we are done"
# Change steps to 30 and observe that the turtle eventually returns. Ask: What is the logic?
Can you tell at what step the turtle would begin to retrace?
###DEMO 10 Logic, Numbered vertices and coloring, stride -> 150
###18*(120-100)=360
from turtle import *
clear()
home()
stride=150
turn=100
steps=30
nstep=0
while nstep <= steps:
if nstep >= 0 and nstep <10:
color('red')
if nstep >= 10 and nstep <20:
color('blue')
elif nstep >= 20 and nstep <30:
color('green')
fd(stride);rt(turn);write(str(nstep+1), font=('Arial', 12, 'normal'))
nstep=nstep+1
### DEMO 11 Turtle finds home, can also run it with turn=110
from turtle import *
clear()
home()
here=pos()
stride=150
turn=100
steps=30
nstep=0
while nstep <= steps:
if nstep >= 0 and nstep <10:
color('red')
if nstep >= 10 and nstep <20:
color('blue')
elif nstep >= 20 and nstep <30:
color('green')
fd(stride);rt(turn);write(str(nstep+1), font=('Arial', 12, 'normal'))
nstep=nstep+1
there=pos()
if there==here:
break
print "Turtle found home in ", steps, " steps"
### DEMO 12 Random Walk!
### special import syntax and function assignment f()
### Change turn to 120 or 90 for constrained random walk, and do not modify strides.
from turtle import *
import random
f=random.random
clear()
home()
here=pos()
stride=50
turn=360
steps=100
nstep=0
while nstep <= steps:
if nstep >= 0 and nstep <10:
color('red')
if nstep >= 10 and nstep <20:
color('blue')
elif nstep >= 20 and nstep <30:
color('green')
fd(stride*f());rt(turn*f())
nstep=nstep+1
there=pos()
if there==here:
break
print "End of Walk reached"
## 12.1 Simpler variant using random color
## You can find length between initial and final point
from turtle import *
import random
f=random.random
fc=random.randint
clear()
home()
here=pos()
stride=50
turn=90
steps=100
nstep=0
COLORS=['red','green','blue']
while nstep <= steps:
cc=COLORS[fc(0,2)]
color(cc)
fd(stride*f());rt(turn*f())
nstep=nstep+1
print "End of Walk reached"