Instructions

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

Exercises

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:

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:

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:

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:

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:

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).