CS21 Lab 3: if/else statements

Due Saturday, February 11 before midnight

This lab assignment requires you to write a few programs. First, run update21. This will create the cs21/labs/03 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/03 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$update21$ cd cs21/labs/03
$pwd /home/[your_username]/cs21/labs/03  We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory! Goals The goals for this lab assignment are: • continue using for loops, accumulators, indexing, and string operators (+,*,:) • practice using if, if-else, and if-elif-else statements • practice using comparison operators like == and < • practice incremental development, testing, and debugging Programming tips As you write your programs, use good programming practices: • For now it's okay to assume that the user gives you valid input, i.e. if your program asks for a 'y' or an 'n', you can assume the user will comply. • Use a comment at the top of the file to describe the purpose of the program. • Use variable names that describe the contents of the variables. • Use def main() to combine the steps of your program into a function. • Write your programs incrementally and test them as you go. This is really crucial to success: test, test again, and then test some more! Don't assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output. 1. Museum ticket pricing Ask the user a series of questions to determine how much they will need to pay for entry to the Swarthmore Museum of Unusual Rectangular Footstools (SMURF). There are different prices for members and non-members, as well as discounts for minors and seniors. There's an additional 30% off the base price for students and educators on top of any other discounts (nice!). This table shows the base prices: Non-memberMember Minor (under 18)$6.00$4.00 Adult (18-64)$15.00$12.00 Senior (65 and over)$8.00$5.50 You can assume the user always enters valid input. Here are some sample runs: $ python smurf-pricing.py
Welcome to SMURF!
Home to Eastern PA's largest collection of unusual rectangular footstools.

Are you a member (y/n)? n
What is your age? 18
Are you a student or educator (y/n)? n

That'll be $15.00 please.$ python smurf-pricing.py
Welcome to SMURF!
Home to Eastern PA's largest collection of unusual rectangular footstools.

Are you a member (y/n)? y
What is your age? 97
Are you a student or educator (y/n)? y

That'll be $3.85 please.  If you're comfortable with string formatting, use it to make sure that the output has a valid dollar amount with two places after the decimal. Put your code in a file called smurf-pricing.py. 2. Buildings Write a program, building.py, that prints out a drawing of a building using dash characters, -, and bar characters, |. (The bar character is usually typed by pressing Shift and \.) Ask the user for the number of floors and the number of windows per floor, as below: $ python building.py
Floors: 4
Windows per floor: 6
-------
|||||||
-------
|||||||
-------
|||||||
-------
|||||||
-------
$python building.py Floors: 1 Windows per floor: 1 -- || --  If the user asks for a building that's 10 or more floors, you should use stars instead of dashes to draw the building floors: $ python building.py
Floors: 12
Windows per floor: 2
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***
|||
***


Normally on lab programs it's okay if your output looks a little different than ours. For this program you should try to get your output to look exactly like ours. Notice that to draw two windows we need three bars. To draw four floors (as in the first example) we need five lines of dashes.

Hints:

• Think about simplifying the pattern as a first step.
• The string repetition operator (*) may be useful.

3. Detect repeats

A pair of characters in a string is a repeat if the two characters are consecutive and the same character. Write a program called countrepeats.py that asks the user for an input string and then counts how many repeats the string contains. Here are some examples:

$python countrepeats.py This program counts the number of repeat characters in a string Enter string: I like puppies and kittens. There are 2 repeats in that string.$ python countrepeats.py
This program counts the number of repeat characters in a string

Enter string: yellow balloons oh my!!
There are 4 repeats in that string.

$python countrepeats.py This program counts the number of repeat characters in a string Enter string: repeat characters are lame There are 0 repeats in that string.  If the input string has a stretch of more than two consecutive, identical characters, each pair counts as a repeat. For example: $ python countrepeats.py
This program counts the number of repeat characters in a string

Enter string: Wooohooo!!!
There are 6 repeats in that string.


4. Secret message

For this problem we have a starting point file, message.py, into which you should add your solution and comments. Add your code after this line in the file:

##### add your soln here:


We are going to give you a very long string assigned to a variable msg. Hidden in that string is a secret message. It is your job to find and print the hidden message.

The letters of the hidden message are evenly spaced throughout the long string. The spacing is given by the number of a's in the string. So, to find the hidden message, your first task is to figure out how many a's are in the string. Once you have that number, use it to find the message.

Here's a small example:

msg = "ssauzatfeiqrbajqrsxwax"


Counting through that string, you'll find that there are 4 a's. To find the message, start at position 4 (since there are 4 a's) and then use every 4th character (since there are 4 a's). So, here is the secret message:

msg = "ssauzatfeiqrbajqrsxwax"


The only requirements for the program are that you don't use advanced python concepts (ones we haven't covered in class yet), and that your program finds and prints the hidden message.

Because it may be difficult to debug your solution when using the very long string, the starting point code allows you to choose to run your program on a much smaller test message string (and you are encouraged to modify this string to test that your solution handles different cases). When you run the program, if you enter 1 your program will use the smaller debug string. If you enter an int value other than 1, your program will use our large message string.

For example, here is output using the debug string:
\$ python message.py
Enter 1 for debug msg or 0 for full message: 1

ssauzatfeiqrbajqrsxwax
------------------------------------------------
zebra


5. Answer the Questionnaire; Run handin21

Once you are confident that all three programs work, fill out the questionnaire in README-03.txt. Then run handin21 a final time to make sure we have access to the most recent versions of the files required for this lab.