## Unit 7 Assignment

Lab 7 has two different parts, each of which is a separate script. Part One uses turtle to draw a checkered pattern using for loops. Part Two is a simple guessing game using a while loop.

## Part 1 Checkerboard

Draw the first row of a checkerboard using for loops We'll be creating the pattern shown above. The pattern below is for the optional challenges. If you're not familiar with checkers, you may wish to see this introduction: rules of checkers .

### Initialization

Create your turtle, hide it, and lift the pen (we'll never put the pen down - we only want fills, not lines). Create a size variable and set its value to 70. This is the length of a side of an individual square in the checkerboard. Create upperLeftX and upperLeftY variables, which represent the coordinates of the upper left corner of the checkerboard pattern. The values for upperLeftX and upperLeftY should be calculated and assigned using the size variable so that the pattern is centered left-to-right and is positioned to be the top row of a complete checkerboard (as shown below for the optional challenges).

#### drawSquare function

Write a drawSquare function having an x parameter, a y parameter, and a color parameter. Send the turtle to the x/y coordinates (which represent the coordinates of the upper left corner of the square currently being drawn), set the fillcolor to the specified color, and begin filling. Then, when you draw the square that is to be filled, use a loop that iterates 4 times. Within the loop, move forward by the size distance, and then turn right 90 degrees. After the loop, end the fill. Note that we aren't actually using the for loop iterator value - we simply use the loop to draw one side of a square four times.

### Drawing the Pattern

Use a loop that iterates 8 times. Give the iterator variable the name column. Within the loop, you first need to decide what color to use (red should be the first color of the pattern). Next (and still within the loop), call the drawSquare function. Remember to change the x parameter value each time you call the function.

Turtle tip: Turtle can sometimes be annoyingly slow - speed things up with this command (replace myturtle with the name you have given your turtle)

```myturtle.speed(0)
```

For a discussion of the turtle speed method see: turtle speed method .

OPTIONAL CHALLENGES: If you would like to extend the basic lab, some possible enhancements are listed below. These are simply for your learning and enjoyment, not for extra credit. You may do any or all of the challenges.

1. Draw the entire checkerboard. You should use two for loops - one inside the other. Think about which loop you want to be the outside loop - the row or the column? It can be a bit tricky to get the colors to alternate properly - try to solve this before resorting to the hidden hint.
Hint: (highlight the following text to view it) Determine the color from the value of (row + column) mod 2. End hint.

2. Add the white and yellow colored checkers as shown - write a drawChecker function and call it from within one or more loops. Checkers diameter should be 80% of the size of a square.

3. Rescale the checkerboard. Change the value of the size variable from 70 to, say 50 or 90. The checkerboard (and any checkers) should redraw correctly while staying centered on the home point.

## Part 2 Guessing game

Write a script that plays a simple guessing game. The script will generate a secret number from 1 to 100, inclusive, and the user will have to guess the number. After each guess, the script will tell the user if they are high, low, or correct. If they are correct, the script will congratulate the user, tell them how many guesses they took, and then end the script. Hint: most of the game code will be in a loop that repeats until the correct number is guessed.

To generate the secret number, you will need to use the randint function from Python's Random module, as follows:

```import random

#this generates a random int from 1-100, inclusive
secretNum = random.randint(1,100)
```

OPTIONAL CHALLENGES: If you would like to extend the basic lab, some possible enhancements are listed below. These are simply for your learning and enjoyment, not for extra credit. You may do any or all of the challenges.

1. After the user has correctly guessed the number, ask them if they would like to play again. If "Y" or "y", play another game. If anything else, quit.

2. Validate the guess. Give an error message for any guess that is lower than 1 or higher than 100, but otherwise ignore the guess.

3. If the guess is within 3 or less (but not if they guessed the secret number), then, after giving the user the high/low message, tell the user "But you're close!". Hint: use the built-in absolute value function abs.