Lab Exercise 3

Practice with Basic Flow of Control


CS 1510
Introduction to Computing


Introduction

By now, you know the drill:

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

Tasks

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.



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:

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

Tasks

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,
  4. Announce to the user that the program won.

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

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.



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