Homework 6 (Python intro) -- due Nov. 20

09 Nov 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!
    
  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)
      

      What does that mean?

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

      ix = int(x)
      iy = int(y)
      iz = int(z)
      
  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.

  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.

  5. Do #5 near the bottom of Chapter 4.

  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.

  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.