Lab 6: Indefinite Loops and Strings
Goals
After the lab, you should be proficient at
- solving advanced conditional problems
- using indefinite loops to solve problems
- solving basic and advanced string problems using methods, loops, and the substring operator
Objective: Review
Review the slides for today.
Objective: Help Client Set Up
Run labhelp
Objective: Linux
Make a directory for this lab.
Copy all the files
from /csci/courses/cs111/handouts/lab6
into
your lab6
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files as lab6_x.py, where x is the problem number.
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. I am getting tougher on these criteria as we develop larger programs.
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 good test cases, and save the output to a file named lab6_x.out, where x is the problem number.
- (12) Last lab, 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!". (You do not need to define a function.)
As always (and I will stop reminding you of these things), consider the user and what is useful output to/information for the user.
- (25) Simulate a simple Craps game (without betting). Before we
talk about the specification, let's talk about your process: 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 that the person's first roll is a 2, 3, 7, 11, or 12. Then, handle the point. In this program, you do not need to define your own functions.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, tell the player what they rolled and 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.
- (14) Translate a string into pirate speak. Write a program that
takes a string as input, converts that string to pirate speak, and
displays it.
To convert a string to pirate speak, replace every 'r' in the string with 'rrr'. (You don't need to worry about uppercase R's, just lowercase.) For example:
Enter a string to convert to pirate speak: generals generrrals
Enter a string to convert to pirate speak: hello there, friends. hello therrre, frrriends.
If the string doesn't have the letter 'r' in it, add an ', arrrr' to the end of the string to make it more pirate like.
Enter a string to convert to pirate speak: washington and lee washington and lee, arrrr
(Did you know that programming had such practical uses?)
- (14) Web servers and Web browsers need to know a file's type so
they know how to display the file. The file's type is determined by
the file name's extension. For example, we name our Python
scripts with the
.py
extension and our HTML files with the.html
extension.Using
help(str)
in the Python interpreter, look at how to use therfind
method and compare with how thefind
method is used.Then, use the
rfind
method to determine any file's type based on its name. (Note: the goal of this problem is quite different from the example code that we examined in class.)Think about good test cases for this functionality. Your code should handle error cases appropriately.
Example output:
What is the name of your file? index.html index.html is a html file.
What is the name of your file? picture.jpg picture.jpg is a jpg file.
What is the name of your file? 15.strings.pdf 15.strings.pdf is a pdf file.
- (17) Open up
pick4.py
(which you copied at the beginning of class). Goal: You're going to build on this program to determine if a user's pick for the number matches the randomly selected number. You can see where we're going with the example output below, but let's break it down!In a new file (named appropriately for the lab), write a function called
generateWinningNumber
that takes no parameters and returns a generated winning number, as astr
. Note that this is a good bit different than what you did on the earlier lab, where the winning number was just printed. Consider: Why should the result be a string? How are you using this "number" as opposed to how you used it before? Note that I didn't mention a test function. Why can't we use thetestEqual
function to test this function?Then, put the other, non-constants/non-imports code into
main
, call your new function where appropriate, and callmain
.Once that is working, check if the user's number matches the winning number. If they match, then the user wins a bunch of money. Otherwise, they lose.
Demonstrate that your code works. (Note: to make demonstrating that your code works easier, you can make changes to the constants. However, make sure that the submitted version of your code has the original, required values for the constants.)
Example runs:
Let's play the Pick 4 lottery! What is your pick? (Format: ####): 0123 The winning number is 5487. Sorry, you didn't win.
Let's play the Pick 4 lottery! What is your pick? (Format: ####): 0123 The winning number is 0123. Congratulations! You won! Let's be friends!
- (18) Write a function called
stretchString
that takes as a parameter a string and returns that string 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....."
You can print out what the function returns. (How?)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.......
Extra Credit (up to 5 pts)
We can provide problem clarifications, but try to solve extra credit problems on your own.
Modify your Super Bowl simulator to be a World Series simulator to a "Best of X" simulator. For example, Major League Baseball's World Series is a best of 7 series; a team wins the series when it wins 4 games. At most 7 games are played. Earlier elimination rounds leading up to the World Series championship may have only at most 5 games.
At the beginning of the lab, you copied superbowl.py
,
which you can use a starting point for your code.
You should keep the constants, but, rather than the number of simulations, you'll need to know the maximum number of games that will be played. (For the World Series, the maximum number of games to play is 7.) You'll also need a constant for what the series is (e.g., "World Series" or "American League Championship")
Save this problem and its output with the prefix lab6_ws_ec.
You should demo at least one series with 5 games and one with 7.
Example Output:
************************************************** This program predicts which team will win the MLB World Series in the best of 7 series ************************************************** Will it be The Orioles or The Pirates? Game 1 : The Orioles win Game 2 : The Orioles win Game 3 : The Orioles win Game 4 : The Orioles win ================================================== The Orioles are predicted to win the MLB World Series in 4 games: 4 - 0
************************************************** This program predicts which team will win the MLB World Series in the best of 7 series ************************************************** Will it be The Orioles or The Pirates? Game 1 : The Orioles win Game 2 : The Pirates win Game 3 : The Orioles win Game 4 : The Orioles win Game 5 : The Pirates win Game 6 : The Pirates win Game 7 : The Pirates win ================================================== The Pirates are predicted to win the MLB World Series in 7 games: 4 - 3
Extra Credit (up to 7 pts)
We can provide problem clarifications, but try to solve extra credit problems on your own.
Sometimes, lotteries pay out money for getting a few matches (the correct number in the same spot). For example, if the user guessed "1234" and the winning number is "1732", then the user had two correct numbers (the 1 and 3 are in the correct place) and maybe wins a little bit of money.
Copy your lab6_5.py
, naming
it lab6_p4_ec.py
. Modify your program such that you have a function that returns how many numbers match between two given numbers.
Then, modify your program such that, if the user matches 2 or 3 numbers,
congratulate the user on getting some numbers correct.
Also add error handling to make sure that the user enters a valid number.
The following are example runs that do not demonstrate all the functionality.
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 1234 The Pick 4 Winner is 8272 You lose! Good thing you didn't bet any money.
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 123 Your pick does not have enough digits
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 1234 The Pick 4 Winner is 0232 You had 2 digits right! Here's a bit of cash.
************************************************** This program simulates the Pick 4 VA Lottery game ************************************************** What is your guess for the Pick 4 number (in format ####)? 1234 The Pick 4 Winner is 4321 You lose! Good thing you didn't bet any money.
Finishing up: What to turn in for this lab
- Move
the original
pick4.py
you copied to the parent directory. You can move the given code (e.g., for the extra credit problems) to the parent directory as well, to declutter your submission.Note that each command links to a page with more information about using the command.
- Create the printable lab assignment, using the
createPrintableLab
command. - View your file using the
evince
command. - Check that the PDF contains all (and only) the necessary files.
- Print the file from evince. You can print to other printers if there are issues with the computer science printers (which do not cost you anything to print computer science work).
- Submit
your lab directory into your
turnin
directory. - Log out of your machine when you are done.
Labs are due at the beginning of Friday's class. You should
hand in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before
class on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 100 pts; see above for breakdown