Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CS1201 Worksheet01

Create a directory called Worksheet01 under your home directory. Change


to that directory and do all your work there. Open a le called Work-
sheet01.txt (using gedit) inside this directory where you will write down
the answers to all questions asked in this worksheet. Once you are done,
archive the directory and upload it to welearn .

Q 1) Using gedit, create a text le called drawPoly.py in the Worksheet01


directory. The le should contain the following lines (you should supply the
necessary indentation) :

from turtle import *


n = input(' Enter the number of sides : ')
l = input(' Enter the length of each side : ')
reset()
angle = 360/n
for i in range(n):
forward(l)
right(angle)
print 'Finished drawing %d-sided regular polygon!'%n
raw_input('Hit enter to quit!')

Run this program (after modifying it properly) by typing python drawPoly.py


at the linux prompt. Supply 100 for l and 5 for n. Edit the program further
so that it will draw an arbitrary sided polygon correctly (as it stands, the
code does not draw, for example, a 7 sided polygon properly). Write down
the change you have made, with explanations, in the le Worksheet01.txt
.

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

>>> from turtle import *


>>> help(up)

You might also like