## Lab Exercise 3

### Introduction

By now, you know the drill:

• Create a directory on your USB device for this lab, say, lab03.
• Launch IDLE by selecting Start | All Programs | Python 3.4 | IDLE (Python GUI).

Keep in mind that you will save your shell window at the end of the session, as a transcript of your interactions throughout the lab. Don't close it along the way!

You do not need a responses.txt file today. I don't ask any questions in these exercises that you won't answer in code!

### Toward a Simple Encryption System

#### Rot13

I have mentioned Rot13 in class a couple of times. It is a simple letter substitution encryption scheme, often used on the internet to obscure the meaning of a text. Rot13 replaces the alphabetic characters in a message with those that are thirteen positions ahead or behind in the alphabet. For example, the letter 'a' is replaced by 'n', and vice versa.

```    a <=> n
b <=> o
c <=> p
...
k <=> x
l <=> y
m <=> z
```

Numbers and punctuation are not encoded.

#### New Python

Your reading for this week introduced the if statement. We used its simplest form in class yesterday. Today, you will also use the else clause.

In Python, you can write combined boolean expressions using the common words and and or:

```    >>> (1 < 2) and (2 < 3)
True
>>> (1 < 2) and (2 < 0)
False
>>> (1 < 2) or (2 < 3)
True
```

You will read more about these for next week. But you can use one of them today. You can also use any functions you've used before, such as ord() and chr().

Now, use your Python shell to grow a short program that implements a portion of Rot13.

1. Open a new program file. Name it rot13.py.

2. Construct a boolean expression that returns True if char is in the first half of the alphabet ('a'-'m') and False otherwise.

3. Use your boolean expression to construct an if statement that changes char to its corresponding encoded letter, by adding or subtracting 13 from its ASCII value.

4. Use your if statement to construct a for loop that encodes each character in a string and prints the encoded character to the screen.

5. Use your for statement as the body of a program that reads a string from the user, encodes each character, and prints the encoded characters to the screen.

You may assume that all of your input is alphabetic characters.

#### Challenges

You might like to work on one or more of these later, just for fun.

• Modify your program to print all of the encoded characters on the same line. You might consider using the running total pattern we saw in class yesterday -- but with a string total instead of a number.
• Modify your program to handle uppercase letters, too.
• Modify your program to handle non-alphabetic characters, too.

### Playing the High-Low Game

#### The Game

The first player chooses a number between 1 and 100. The second player tries to guess the number. She is allowed to one question: Is your number X? In response, the first player says one of three things:

• yes, if the guess is correct
• higher, if the player's number is higher than the guess
• lower, if the player's number is lower than the guess

Unless the second player has an especially bad strategy for guessing, the game takes at most 100 guesses. But a good strategy can do much better!

#### New Python

Your reading for this week introduced the while statement. Its form is identical to the simple if statement. For example:

```    >>> number = 5
>>> while number > 0:
print(number)
number = number - 1

5
4
3
2
1
>>> _
```

Like an if, the while statement executes its suite of statements if its condition is true. But it keeps doing it as long as its condition is true.

Notice what this means... The suite of statements must do something to change the condition's value, or the loop continues forever! Try this:

```    >>> number = 5
>>> while number > 0:
print(number)

5
5
5
5
5
...
Traceback (most recent call last):
...
>>> _
```

I had to hit Control-c to break out of the loop.

You will read more about these for next week. But you can use one of them today, in conjunction with functions you've used before, such as ord() and chr()

Open a new program file. Name it hi_lo.py. Write a program that plays the role of the second player -- the guesser -- in the High-Low game. The user of the program is the first player.

Your program should behave as follows:

1. Decide on its first guess.
2. Ask the user Is your number X?, putting its guess in for the X.
3. While the user responds something other than yes,
• Decide on its next guess, perhaps using the answer of higher or lower in the process.
• Ask the user Is your number X?, putting its new guess in for the X.
4. Announce to the user that the program won.

Your program can create its new guess any way you want. Here are two suggestions.

• Simple guessing. Start with a guess of 1. Create the next guess by adding 1 to the previous guess.

• Complex guessing. Use variables to store the smallest possible answer (1) and the largest possible answer (100). Start with a guess of 50, right in the middle.

If the user responds higher, update the smallest possible answer to be the guess + 1. Create the next guess by choosing a number in the middle of the smallest and largest possible answers.

If the user responds lower, update the largest possible answer to be the guess - 1. Create the next guess by choosing a number in the middle of the smallest and largest possible answers.

Feel free to use the simple strategy! It is worth full credit.

#### Challenges

You might like to work on one or more of these later, just for fun.

• Implement both strategies.
• Modify the programs to count and print the number of guesses the program makes before getting the answer.
• Run both programs using the same correct answer. Compare the number of guesses made by the two programs.
• Do the same with several correct answers, including at least a few big numbers, such as one million. What do you notice?

### Finishing Up

Make sure that your program files are complete and saved. Save your shell window to a file called transcript.py.

Submit your files for grading on the electronic submission system, at lab03 -- Practice with Basic Flow of Control. As always, make sure you see the verification screen, such as:

```    Uploading the following files.

transcript.py of size 1467 bytes
rot13.py of size 616 bytes
hi_lo.py of size 914 bytes
The files listed above were uploaded.
You are responsible to make sure that this list is complete.
You may replace or add file up until the deadline by using this system again...
```

If you need any help, let me know.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 10, 2014