# CS21 Lab2: Numbers and Strings

Due Saturday (February 7) before midnight

This lab assignment requires you to write three programs in Python. First, run update21. This will create the cs21/labs/02 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/02 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/02
$pwd /home/your_user_name/cs21/labs/02  We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory! Programming tips As you write your first programs, start using good programming practices now: • 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 on them. 1. Farmers' Market Produce cost Write a program that computes the average cost per pound of a set of items. Your program should first prompt the user for the number of unique items purchased. For example, if the user purchased beets, bananas, and berries, the user should enter 3. Next, prompt the user for the number of pounds of each item purchased and the price per pound. Your program should then compute the total cost of the purchase and the average price per pound. Your program should use print formatting to show the values with two decimal places. Here are some sample runs showing how your program should work: $ python produce.py

Enter number of unique items: 1

Enter item weight in pounds: 2
Enter cost per pound: 0.75

Total cost is $1.50 Average cost per pound is$0.75


$python produce.py Enter number of unique items: 3 Enter item weight in pounds: 1 Enter cost per pound: 3.50 Enter item weight in pounds: 2 Enter cost per pound: 0.80 Enter item weight in pounds: 3 Enter cost per pound: 1.00 Total cost is$8.10
Average cost per pound is $1.35  Text Stairs Write a program that asks the user to enter a text phrase and converts the characters to a step pattern. Your program should ask the user for the width, in number of characters, for each step. The last step may have fewer than width characters if the width does not divide the length of the phrase evenly. You may wish to handle the last step separately from the rest. Here's how the program should work: $  python textstairs.py
Enter phrase: The.boats.on.the.bayou.float.right.by.you
Enter stair width: 5
The.b
oats.
on.th
e.bay
ou.fl
oat.r
ight.
by.yo
u


$python textstairs.py Enter phrase: Pawnee Parks and Recreation Enter stair width: 3 Paw nee Pa rks an d R ecr eat ion  3. Text Snowflake Write a program to print a snowflake text pattern of asterisks given a user specified size. Below are some sample runs showing how your program should work. $ python snowflake.py
Enter size of snowflake: 3

*  *  *
* * *
***
*******
***
* * *
*  *  *

$python snowflake.py Enter size of snowflake: 7 * * * * * * * * * * * * * * * * * * *** *************** *** * * * * * * * * * * * * * * * * * *  The snowflakes look a little compact for small values of size. $ python snowflake.py
Enter size of snowflake: 0
*

$python snowflake.py Enter size of snowflake: 1 *** *** ***  $ python snowflake.py
Enter size of snowflake: 2
* * *
***
*****
***
* * *

A snowflake of size $n$ should have $n$ rows above the center row and $n$ rows below the center row. The top and bottom halves of the snowflake should each have three branches. Hint: Use separate for loops for the top and bottom halves and print the middle row outside the loops.
Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.