Lab 6: Advanced Conditions, 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
- pair programming methodology
Objective: Review
Review the slides for today.
Objective: Linux
Make a directory for this lab.
Copy all the files
from /csdept/courses/cs111/handouts/lab6
into
your lab6
directory.
Objective: Help Client Set Up
Run runHelpClient &
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.
All problems should be completed using pair programming with your assigned partner.
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.
- (15) 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, at which point the program will print "Eureka!".
- (15) Download/copy
the pick4winner.py
file from yesterday. Refactor the code such that it has a
function called
generateWinningNumber
that takes no parameters and returns a generated winning number. (Why can't we use thetestEqual
function to test this function?) Then, put the other, non-constants/non-imports code intomain
and callmain
. Demonstrate that your code works. - (15) 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: hello there, friends. hello therrre, frrriends.
Enter a string to convert to pirate speak: generals generrrals
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?)
- (15) 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 based on
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. 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.
- (15) Write a function called
reverseString
that returns a string (passed in as a parameter) backwards. (Recall good practices for writing functions.) Implement this to simulate how you would reverse a string; don't use any tricks that you find on the web or that a student assistant suggests.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 string in reverse.Example output:
What is your string? The Beatles Your string in reverse is seltaeB ehT
What is your string? Help! Your string in reverse is !pleH
- (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 (a function may be appropriate.)
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 first roll 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.
Note that you copied the file
game.py
, which has a useful function that you should use when writing this program. Recall: How do you use code from other files in your program?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.
Extra Credit (up to 7 pts)
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 "1784", then the user had two correct numbers and maybe wins a little bit of money.
Modify your pick4winner.py
(that you refactored above)
such that it reports how many numbers the user matched. If the user
matched 2 or 3 numbers, congratulate the user on getting some
numbers correct.
Include error handling to make sure that the user enters a valid number.
Save this problem and its output with the prefix lab6.ec.
************************************************** 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.
Finishing up: What to turn in for this lab
- When you, as a pair, are ready to submit OR if you are at the
end of the lab period, run
pairturnin.sh labx partnerusername
wherelabx
is the name of the lab you are submitting andpartnerusername
is your partner's username on the lab machines (the person whose account you are not using to write the code). For more information about the command, see the wiki.If you want to copy your pairs' work into your
cs111
directory--either just to have it or to work on your code on your own--use the scriptstartup.sh
. Run this command from the account that you want For example, run startup.sh labx partnerusername
wherelabx
is the name of the lab you're working on andpartnerusername
is your partner's username on the lab machines.For more info, see the wiki.
- If you complete the lab on your own after the lab
period, submit
your lab into your
turnin
directory. - Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Print the file using
the
lpr
command. - Log out of your machine when you are done.
Perform the following steps from
your cs111
directory.
Note that each command
below links to a page with more information about using the
command.
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