Lab 7: Indefinite Loops, Strings, and ASCII
~~ Staggered Extension ~~
- Problems 1-7 are due Friday, as usual.
- Problems 8, 9, Extra Credit are due on Monday before class.
Goals
After the lab, you should be proficient at
- using indefinite loops to solve problems
- using escape sequences
- using the ASCII representations of characters with built-in functions
- formatting strings
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
runHelpClient - Create a directory for this lab.
- Copy all the
.py
files in/csdept/courses/cs111/handouts/lab7/
into yourlab7
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file.
Your programs will be graded on correctness, style, efficiency, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart
IDLE or close and reopen the IDLE "shell" by running the program again
(using F5), demonstrate that the program works using
several
- (12) Previously, we wrote a program that displayed "Eureka!" or another message for certain values. This time, write a program that repeatedly prompts the user for a number until that number is between 500 and 1000, inclusive, at which point the program will print "Eureka!".
- (25) Simulate a simple Craps game (without betting). Break
the game into manageable pieces and build up. For example, make
your program simulate rolling two dice (recall: you copied
the
game.py
module at the beginning of the lab) and then test your program. Then, handle when the person first rolls a 2, 3, 7, 11, or 12. Then, handle the point.The Rules
- A player rolls two dice.
- There are three possibilities:
- 7 or 11 wins. If the total of the dice is 7 or 11, then the player wins.
- 2, 3, or 12 loses. If the total of the dice is 2, 3, or 12, then the player loses.
- Others become the point. If the total is
any other number (4, 5, 6, 8, 9, 10), then this number
becomes the point.
The player keeps rolling until one of two things happen. Either the player makes the point and wins, or the player rolls a 7 and loses (craps out). Any number other than the point or 7 is of no consequence.
After the first roll, print a message that tells the player that they either won, lost, or which "point" they have to play for.
Example runs:
*** This program simulates Craps (without the betting) *** You rolled a 11 Congratulations! You win!
*** This program simulates Craps (without the betting) *** You rolled a 6 The POINT is 6 Roll again! You rolled 4 Roll again! You rolled 6 Congratulations! You win!
*** This program simulates Craps (without the betting) *** You rolled a 10 The POINT is 10 Roll again! You rolled 4 Roll again! You rolled 9 Roll again! You rolled 7 Sorry! You crapped out. Luckily, money wasn't involved.
- (18) Write a function called
stretchString
that returns the single string (provided as a parameter) stretched by inserting periods between each letter. One period should appear after the first letter, two dots after the second letter, and n dots after the nth letter. (Recall good practices for writing functions.)For example, if I call
stretchString("cs")
, the function should return"c.s.."
. If I callstretchString("cs111")
, the function should return"c.s..1...1....1....."
Test that your function works using the
test.testEqual
function. Put these tests in a test function, as you did in previous labs.After confirming that your function works, create a
main
function that prompts the user for input and displays the user's input stretched.Example output:
This program stretches a word that you provide. Enter your word: cs The stretched word is c.s..
This program stretches a word that you provide. Enter your word: monkeys The stretched word is m.o..n...k....e.....y......s.......
- (13) Write a function called
encodeLetter
that takes as parameters a lowercase letter and a key and returns the letter encoded. For example, if you call the functionencodeLetter('a', 1)
, the function should return 'b'. Similarly, if you call the functionencodeLetter('b', -1)
, the function should return 'a'.As we've been doing, write the function and test it using the
test.testEqual
function in a function calledtestEncodeLetter
. What are good test cases for this function?No main or user input for this program... We're building up to that. No output for this program, unless you want me to see a bunch of "Pass" messages.
- (13) Copy the previous program and add a function
called
encodeMessage
that takes a message to encode and a key as parameters and returns the encoded message. For example, if you callencodeMessage("w and l", 1)
, the function should return"x boe m"
.The function you created in the last problem will likely be helpful to call. (Read: you should call that function in this function.) You should NOT modify the function you wrote in the previous problem. You already know that's working, so don't modify that function.
Note that spaces will have to be handled specially.
As before, create a function that tests this function. What are good test cases for this function?
Still no main or user input for this program... We're building up to that. No output, unless you want to show all of your test cases pass.
- (13) Copy the previous program and create your final Caesar
Cipher program. The program will take as input a phrase to encode
and a key and will display the encoded phrase.
Your program should handle the error case where the
user-supplied key is not in the valid range.
Your program can assume that messages only contain lowercase letters and spaces.
Give your neighbor a message to decode, along with the original key used to encode the message. Put your neighbor's name, the original message, and the decoded message in comments in your finished program, right under the high-level description. If others aren't around, decode this message, which I generated using the key 12:
bkftaz ue yk rmhadufq bdasdmyyuzs xmzsgmsq
Show your code decoding the message in your saved output.Your program should produce the following interaction with the user:
This program encodes and decodes Ceasar ciphers. Enter some text: i like the lions at the zoo Enter an integer key (between -25 and + 25): 1 The encoded text is: j mjlf uif mjpot bu uif app
This program encodes and decodes Ceasar ciphers. Enter some text: j mjlf uif mjpot bu uif app Enter an integer key (between -25 and + 25): -1 The encoded text is: i like the lions at the zoo
- (10 pts) Using a
for
loop, draw a diagonal line that looks like:\ \ \ \ \
Think about the pattern of what is getting printed.
After you have that working, have the user enter the size of the diagonal line and draw a line of the appropriate size.
- (8 pts) Copy
lab2/lab2.2.py
into this directory and name it appropriately for this lab. Update the comment appropriately. Modify it such that the program always displays the money values to 2 decimal places. Note the change with the placement of the dollar sign.Output should look like:
Bill amount in dollars: 55.34 Percent tip: 20 Number of people: 3 The tip is $11.07. The total cost is $66.41. The cost per person is $22.14.
Bill amount in dollars: 162.29 Percent tip: 15 Number of people: 8 The tip is $24.34. The total cost is $186.63. The cost per person is $23.33.
- (18 pts) Write a program that creates a table of Olympic
competition running distances in meters, kilometers, yards, and
miles. The following distances should be used: 100 m, 200 m, 400
m, 800 m, and 1600m.
Note that 1 m = .001 km = 1.094 yds = .0006215 mi
Calculate and display the results, formatted in the following manner:
Meters Kilometers Yards Miles --------------------------------------- 100 0.100 109.4 0.062 200 ----- ----- ----- 400 ----- ----- ----- 800 ----- ----- ----- 1600 ----- ------ -----
Note: Make sure your output is formatted exactly as above except that you will have all the converted values filled in. Note the spacing of the columns, the justification of the columns, the precision, ...
Your final solution should not have a lot of repeated code. How can you solve this problem without repeated code?
You should not use a list to solve this problem.
Caesar Cipher
Pedagogical Overview: We're going to build up the Caesar cipher code in a couple of steps. By the end of the term, you should feel comfortable with breaking up a problem yourself. In the meantime, we'll go through a few examples of breaking up a program into components that are easier to solve. You can peek at the last Caesar Cipher Code problem to see where we're going.
A Caesar cipher is a simple substitution cipher based on the idea of shifting each letter of a text message a fixed number (called the key) of positions in the alphabet. Recall that the alphabet wraps around. For example, 'z' with a key of 5 is encoded as 'e'.
Rhetorical question: why is the limit on keys between -25 and +25?
Extra Credit
Vignere Cipher (up to 8 pts)
We can provide problem clarifications, but I'd like you to try to solve extra credit problems on your own.
Write a program that implements
a Vigenere
cipher. Name the Python file vignere_ec.py
. You
should be able to leverage your previous code to solve this problem.
Some example output:
This program encodes Vignere ciphers. Enter some text: the eagle flies at midnight Enter a keyword: key Encoded text: dlc iyqpc jjsiq er qgnrgqlr
"Best of" Simulator (up to 6 points)
Write a "best of" simulator that simulates two teams playing a "best of" series.
Copy your Championship/March Madness simulator from Lab 5 (problem
7) and name it simulator_ec.py
. Keep most of the constants and your boolean
function. Change the simulator so that it stops after a team has won
the required number of games in the series. Have constants that
represent the maximum number of games to play and the name of the
series. Update the output accordingly. Some example output:
Play the Best of 7 Series in the World Series -------------------------------------------------- Simulation 1 : Pirates wins Simulation 2 : Orioles wins Simulation 3 : Orioles wins Simulation 4 : Orioles wins Simulation 5 : Orioles wins -------------------------------------------------- Orioles won the World Series 4 - 1
Play the Best of 5 Series in the Wild Card Playoff -------------------------------------------------- Simulation 1 : Orioles wins Simulation 2 : Pirates wins Simulation 3 : Pirates wins Simulation 4 : Orioles wins Simulation 5 : Pirates wins -------------------------------------------------- Pirates won the Wild Card Playoff 3 - 2
Finishing up: What to turn in for this lab
Problems 1-7 are due Friday. Problems 8, 9, Extra Credit are due on Monday before class.
Follow these steps for both the Friday and Monday deadlines.
- Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Submit
your lab directory into your
turnin
directory. - Log out of your machine when you are done.
Note that each command links to a page with more information about using the command.
Problems 1-7 are due Friday. Problems 8, 9, Extra Credit are due on Monday before class.
Ask well before the deadline if you need help turning in your assignment!
Grading (130 pts)
- Python programs: 130 pts; see above for breakdown