Homework 6 (Python intro) -- solutions

15 Dec 2011

See the week 11 agenda for information on installing Python. Many of these exercises follow along with parts of the book.

1

Write a program that when you run it, asks you for your name, then says hello to you, and then goodbye to you. Here's a possible example of the output:

   What is your name? Joel
   Hi Joel!
   Bye Joel!
name = input('What is your name? ')
print('Hello ' + name + '!')
print('Goodbye ' + name + '!')

2

At the interactive prompt, enter the following commands:

   x = '11.5'
   y = '12'
   z = 13.5
  • What is the output when you type each of the following?

     type(x)
     type(y)
     type(z)
    

    str, str, and float

    What does that mean?

    Those are the types of each value.

  • What happens when you type each of the following? Why?

     ix = int(x)
     iy = int(y)
     iz = int(z)
    

    The first is an error, because Python doesn't know how to convert a str to an int if it contains non-digit characters. The second converts y to the corresponding int value. The third converts z from a float to an int by dropping the fractional part.

3

Define a function called square that takes a single argument sidelen and draws a square with side length sidelen (like what is done in the book).

Now define another function called nsquare that takes a side length and an integer n as input, and draws n squares of the given size. Make sure that you move the cursor somehow so that you can see all n squares.

from turtle import *

def square(sidelen):
    for x in range(4):
        forward(sidelen)
        right(90)
        
def nsquare(sidelen, n):
    for s in range(n):
        square(sidelen)
        # rotate so that we can see each square,
        # and so we make a complete circle by the end
        right(360/n)

4

Define a function called ngon with inputs n and s that draws a regular n-sided polygon with side length s. So if n=3, it draws an equilateral triangle, if n=5 it draws a pentagon, etc. Hint: all the turns you make have to add up to 360. This is #3 from Chapter 4.

from turtle import *

def ngon(n, s):
    for side in range(n):
        forward(s)
        right(360/n)

5

Do #5 near the bottom of Chapter 4.

# Example that can be run from command line, specifying angle

from turtle import *
import sys

# make drawing fast
speed(0)
tracer(10)

def spiral(sides, angle):
    length = 2
    for s in range(sides):
        forward(length)
        right(angle)
        length = length + 2
        
# First version:
# spiral(100, 90)
# second version:
# spiral(100, 91)

# Use first command line argument to define angle
angle = float(sys.argv[1])
spiral(100, angle)

# to run as command line program, need 
# this call to keep window open:
mainloop()

6

Experiment with other spiral drawing ideas. Change the colors as you iterate through each line you draw. Try different turning angles for each iteration. Submit the program that you create that draws the most interesting picture.

from turtle import *

# make drawing fast
speed(0)
tracer(10)

# Specify colors using numbers in range 0...255
colormode(255)

# Original spiral
def spiral(sides, angle):
    length = 2
    for s in range(sides):
        forward(length)
        right(angle)
        length = length + 2

# Draw spiral with varying colors
def spiral1(sides, angle):
    # red, green, and blue color components
    r = 0
    g = 0
    b = 0
    length = 2
    for s in range(sides):
        color(r, g, b)
        forward(length)
        right(angle)
        length = length + 2
        # change color components so that we cycle through a range of colors;
        # values chosen just so that colors don't repeat too often.
        r = (r + 11) % 256
        g = (g + 29) % 256
        b = (b + 71) % 256

7

Create a program that draws 1000 lines randomly across the screen. You can use the randint function from the random module to help with this. To import that function, include the line:

   from random import randint

Optional: change the color for each new line; change the thickness of each line. Hint: to make sure that your turtle doesn't just wander off, you'll have to use functions that make it go to a specific location on your screen; try using the goto function, and provide inputs that will make the turtle go to a random location by creating the inputs using randint.

from turtle import *
from random import randint

speed(0)
tracer(10)
colormode(255)

# Draw n random lines with x and y in range [-100, 100]
def randlines(n):
    for line in range(n):
        # choose random color
        color(randint(0, 255), randint(0, 255), randint(0, 255))
        # choose random width
        width(randint(1, 5))
        # choose random initial and final position
        x0 = randint(-100, 100)
        y0 = randint(-100, 100)
        x1 = randint(-100, 100)
        y1 = randint(-100, 100)
        # pick up pen and go to starting position
        up()
        goto(x0, y0)
        # put down pen and go to final position
        down()
        goto(x1, y1)
           
# try calling: randlines(1000)