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!
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.
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.
You may assume that all of your input is alphabetic characters.
You might like to work on one or more of these later, just for fun.
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!
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:
Your program can create its new guess any way you want. Here are two suggestions.
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.
You might like to work on one or more of these later, just for fun.
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.