Lab 6: Indefinite Loops and Strings

Goals

After the lab, you should be proficient at

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.

  1. (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!". (Does not need functions.)

    As always (and I will stop reminding you of these things), consider the user and what is useful output to/information for the user.

  2. (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 the the person's first roll is a 2, 3, 7, 11, or 12. Then, handle the point. This program does not need functions.

    The Rules

    1. A player rolls two dice.
    2. 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.
    
  3. (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: 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?)

  4. (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 the rfind method and compare with how the find method is used.

    Then, use the rfind method to determine any file's type based on its name. (Note: this 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.
  5. (15) Open up pick4_winner.py (which you copied at the beginning of class). In a new file (named appropriately for the lab), 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 the testEqual function to test this function?) Then, put the other, non-constants/non-imports code into main, call your new function where appropriate, and call main. Demonstrate that your code works.
  6. (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 call stretchString("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.......
    

Extra Credit (up to 5 pts)

Modify your March Madness simulator 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 rounds leading up to the World Series championship may have only 5 games.

At the beginnng of the lab, you copied march_madness.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 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               
**************************************************
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               
**************************************************
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)

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.

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.

Also add error handling to make sure that the user enters a valid number.

Save this problem and its output with the prefix lab6_p4_ec.

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

  1. Move the original pick4winner.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.

  2. Create the printable lab assignment, using the createPrintableLab command.
  3. View your file using the evince command.
  4. Check that the PDF contains all (and only) the necessary files.
  5. Print the file from evince. Now that we have Papercut installed on the lab machines, you can print to other printers if there are issues with the computer sciene printers (which do not cost you anything to print computer science work).
  6. Submit your lab directory into your turnin directory.
  7. 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)