Homework 6 (Python intro) -- due Nov. 20
09 Nov 2011See the week 11 agenda for information on installing Python. Many of these exercises follow along with parts of the book.
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!
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)
Define a function called
square
that takes a single argumentsidelen
and draws a square with side lengthsidelen
(like what is done in the book).Now define another function called
nsquare
that takes a side length and an integern
as input, and drawsn
squares of the given size. Make sure that you move the cursor somehow so that you can see alln
squares.Define a function called
ngon
with inputsn
ands
that draws a regularn
-sided polygon with side lengths
. So ifn=3
, it draws an equilateral triangle, ifn=5
it draws a pentagon, etc. Hint: all the turns you make have to add up to 360. This is #3 from Chapter 4.Do #5 near the bottom of Chapter 4.
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.
Create a program that draws 1000 lines randomly across the screen. You can use the
randint
function from therandom
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 usingrandint
.