Work on this lab with your partner (or on your own if you have not been assigned one).

Read over the examples which include the solutions to the pre-lab exercises.

Work from the supplied starter file.

Unless otherwise indicated, use only the Python operations and commands we have described thus far in class.

Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.

In the common case, you will not complete the exercises during our scheduled lab. It is then up to you to complete the lab on your own time.

You are welcome (encouraged) to download a runnable solution module called

`lab2x`

. If you place that file in the same folder as the file you are working in, then from the IDLE prompt (`>>>`

) you can`import`

the solution module to see it in action. For example:`>>> import lab2x >>> lab2x.compute_check_digit(435) 3`

Do some research on leap years. You know that 2000, 2004, 2008, and 2012 are all leap years and none of the years in between are. But did you know that 1900 is

*not*a leap year? Write two equivalent functions that return`True`

if the year is a leap year and`False`

otherwise.Complete

`is_leap_multiway(year)`

so it uses a multiway-conditional statement (`if/elif/else`

) and does*not*use logical operators (`and`

,`or`

). Examples:`>>> is_leap_multiway(2015) False >>> is_leap_multiway(2016) True >>> is_leap_multiway(1900) False >>> is_leap_multiway(2000) True`

Complete

`is_leap(year)`

so it consists of a single`return`

statement. It should make use of the logical operators (`and`

,`or`

,`not`

). Examples:`>>> is_leap(2015) False >>> is_leap(2016) True >>> is_leap(1900) False >>> is_leap(2000) True`

This exercise imagines a world with only the three primary colors (red, yellow and blue) and an encoding between those colors and the numbers 0, 1 and 2:

`0 'red' 1 'yellow' 2 'blue'`

Complete

`color_to_code(color)`

so it returns the numeric coding for the color assuming it is the string`'red'`

,`'yellow'`

or`'blue'`

. Otherwise it returns`None`

. Examples:`>>> color_to_code('red') 0 >>> color_to_code('yellow') 1 >>> color_to_code('blue') 2 >>> color_to_code('white') is None True`

Complete

`code_to_color(code)`

so it returns the color string corresponding to the numerical`code`

. If the code is not 0, 1 or 2 it returns`'black'`

. Examples:`>>> code_to_color(0) 'red' >>> code_to_color(1) 'yellow' >>> code_to_color(2) 'blue' >>> code_to_color(7) 'black'`

Complete

`next_color(color)`

so it returns the next color in the color sequence red, yellow, blue. The next color after blue should be red again. You may assume that`color`

has the value`'red'`

,`'yellow'`

or`'blue'`

. Examples:`>>> next_color('red') 'yellow' >>> next_color('yellow') 'blue' >>> next_color('blue') 'red'`

*Hint*: you can accomplish this with a single return statement using composition, the two previous functions, and the remainder operator (`%`

).

Complete

`first_digit(n)`

so it returns the leading (most significant) digit of`n`

. You can assume`n`

is positive or 0. Examples:`>>> first_digit(0) 0 >>> first_digit(9) 9 >>> first_digit(537) 5 >>> first_digit(123456789) 1`

Complete

`has_repeats(s)`

so that it returns`True`

if`s`

has any consecutive occurrences of the same character, and otherwise returns`False`

. Examples:`>>> has_repeats('') False >>> has_repeats('x') False >>> has_repeats('abab') # not consecutive repeats False >>> has_repeats('abba') True >>> has_repeats('doh!!') # works on any characters True >>> has_repeats('extra spaces') # even spaces True`

Complete

`is_balanced(s)`

so that it returns`True`

if the parentheses in string`s`

are balanced: for every open (left) parenthesis there is a corresponding close (right) parenthesis, and at no point reading from left to right there are more close parentheses than opens. Examples:`>>> is_balanced('') # no parentheses at all True >>> is_balanced('abracadabra') # also none True >>> is_balanced('un(closed') False >>> is_balanced('un)opened') False >>> is_balanced('out)of(order') False >>> is_balanced('((()()()))') True >>> is_balanced('(- (* 5 5) (* 4 4))') True`

Complete

`sum_digits(n)`

so it returns the sum of the digits of`n`

. Examples:`>>> sum_digits(0) 0 >>> sum_digits(8) 8 >>> sum_digits(55) # 5 + 5 10 >>> sum_digits(205) # 2 + 0 + 5 7`

The sum of the digits of a number can be used as a way of verifying that a number received via electronic communication has been transmitted correctly. For example, if we wish to send the number 213, we could send 2136 using the last digit as a

*checksum*(or*check digit*) that the recipient can use to verify that the first three digits were received correctly.We can use the

`sum_digits`

function to compute this checksum, except that we want the checksum to be only one digit long. For example, for the number 815, the sum of the digits is 14, but the one-digit checksum is 5 since it is the sum of the digits 1 and 4.Complete

`compute_check_digit(n)`

so it returns a one-digit checksum corresponding to that number. (I recommend making your function repeatedly call`sum_digits`

until the result is a single digit.) Examples:`>>> compute_check_digit(0) 0 >>> compute_check_digit(55) # 5 + 5 = 10 --> 1 + 0 = 1 1 >>> compute_check_digit(999999999999) # 9 * 12 = 108 ... 9 >>> 99999999999999999999991 # 199 --> 19 --> 10 --> 1 1`

Try using the function

`human_guesses()`

. It is a very simple guessing game and not well implemented at that. Modify that game so that the computer "thinks" of a different number each time (*hint*: use`random.randrange`

). Add code so that when the player finally guesses the correct value, the total number of guesses is reported. Add code so that if the player guesses`0`

it tells the computer to quit the game rather than to keep playing. Here are two possible transcripts of a correctly functioning game:`>>> human_guesses() enter your first guess: 9 enter your next guess: 6 enter your next guess: 3 enter your next guess: 7 you got it in 4 guesses >>> human_guesses() enter your first guess: 5 enter your next guess: 0 you quit`

Try using the function

`computer_guesses()`

. Again, it plays a very simple guessing game and, again, it is not fully functional as given. This time around, the computer tries to guess a number that the human is thinking of. Modify the game so that it works. Add code so that the computer can tell if the human user cheats (i.e., if the computer tries all the numbers between 1 and 10 and none are the answer it can suspect the human is not playing by the rules). Here are two possible transcripts of a correctly functioning game:`>>> computer_guesses() is it 1? n is it 2? n is it 3? n is it 4? y i got it! >>> computer_guesses() is it 1? n is it 2? n is it 3? n is it 4? n is it 5? n is it 6? n is it 7? n is it 8? n is it 9? n is it 10? n you cheated!`

Add code to

`turtle_repl()`

so that it uses a text-based REPL to let the user control the movement of a graphics turtle. You can customize this as you see fit, but I recommend at least allowing these commands:`e: go east w: go west n: go north s: go south q: quit`

You are encouraged to try the instructor's version (you can

`import lab2x`

and then run`lab2x.turtle_repl()`

at the`>>>`

prompt).