Skip to main content.

Lab 6: Advanced Conditions, Indefinite Loops, and Strings

Goals

After the lab, you should be proficient at

  1. solving advanced conditional problems
  2. using indefinite loops to solve problems
  3. solving basic and advanced string problems using methods, loops, and the substring operator
  4. 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.

  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!".
  2. (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

    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, 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.
    
  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: 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?)

  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. 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) 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 the testEqual function to test this function?) Then, put the other, non-constants/non-imports code into main and call main. Demonstrate that your code works.
  6. (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 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 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

  1. 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
    where labx is the name of the lab you are submitting and partnerusername 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 script indiv_startup.sh. Run this command from the account that you want. For example, run indiv_startup.sh labx partnerusername
    where labx is the name of the lab you're working on and partnerusername is your partner's username on the lab machines.

    For more info, see the wiki.

  2. If you complete the lab on your own after the lab period, submit your lab into your turnin directory, as we had before this lab.
  3. Perform the following steps from your cs111 directory.

    Note that each command below links to a page with more information about using the command.

  4. Create the printable lab assignment, using the createPrintableLab command:
    createPrintableLab <labdirname>
  5. View your file using the evince command.
  6. Print the file using the lpr command.
  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)