Professional Documents
Culture Documents
Ws 01
Ws 01
1
Q 2) The program squares.py denes a function called square() that takes
an argument l and draws a square with sides l pixels long and returns the
area of the square. Modify this to create a program poly.py which will take
in the side length l and the number of sides n from the user and draw a n
sided polygon. You will of course have to change the function (along with
its name!) in the program itself.
Note : all your programs should be adequately commented. Having said that,
the amount of commenting in the squares.py program may be a bit in the
excessive side :-) !
Q 3) Write a function called gap() which will take a single argument l and
move the turtle forward by l pixels without leaving a mark on the screen (you
will need to use the up() and down() functions from the turtle module).
Write a program called dashedCircle.py that denes this function, and in
addition contains the following lines :
for i in range(20):
if i%2 == 0:
forward(20)
else:
gap(20)
along with one more line of code (that you will have to add). This program
should, when run, draw a dashed circle (actually a dashed regular 20 sided
polygon but this will look like a circle unless you look at it too carefully) .
Q 4) Write a new version of the square() function (the one that you found
in squares.py) which draws a square of length l, with center at (x, y) with
the rst side making an angle (in degrees) with the horizontal (as shown
in the gure) after being given these values as arguments.
2
l
(x, y)
You will need to use the turtle functions up(), down(), goto() and set-
heading(). 1
The function should take l = 100, (x, y) = (0, 0) and = 0 as default values.
Using this function, write a program nestedSquares.py that draws the
following gure :
1 To understand what these functions do, use the help function from the interpreter.
For example, to see what the function up() does, start the interpreter, and issue the
commands