CS21 Lab 2: Numbers and Strings

Due Saturday, September 21, before midnight

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.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • 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.

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/02 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/02
$ pwd
/home/username/cs21/labs/02
$ ls
Questions-02.txt
(should see your program files here)

Lab goals

The goals for this lab assignment are:

  • Manipulate Python numeric and string data types

  • Learn to apply the accumulator pattern to numeric and string types to solve problems

  • Learn to iterate over data using for loops

  • Practice string operations +, len(), []

1. Farmers' Market

A vendor at a local farmers' market wants a program to tally sales of apples over the harvest season. Write a program called market.py that computes the total sales over a number of days entered by the user. The program should prompt the user for the number of bushels of apples sold per day and the price per bushel each day. Before exiting, the program should print the total number of bushels sold and the total amount of sales in dollars.

Two examples of the running program are shown below. User input is shown in bold.

$ python3 market.py
Enter number of sales days: 2

Enter number of bushels sold: 3.5
Enter price per bushel: 24

Enter number of bushels sold: 2
Enter price per bushel: 22.50

Total bushels sold: 5.5
Total sales: $129.0

$ python3 market.py
Enter number of sales days: 3

Enter number of bushels sold: 1
Enter price per bushel: 20

Enter number of bushels sold: 1
Enter price per bushel: 20

Enter number of bushels sold: 1
Enter price per bushel: 20

Total bushels sold: 3.0
Total sales: $60.0

2. Text Slant

Write a program called txtslant.py that prompts the user for a string, and then prints one character of the string per line in a diagonal pattern as shown below.

$ python3 txtslant.py
Enter a phrase: weee!
w
 e
  e
   e
    !

$ python3 txtslant.py
Enter a phrase: autumnal equinox
a
 u
  t
   u
    m
     n
      a
       l

         e
          q
           u
            i
             n
              o
               x

3. Text Weave

Write a program called txtweave.py that prompts the user for a string msg, and then prints out a new string consisting of the characters in msg in forward and reverse interleaved. For example, if the user enters the string hello, you would take the letters in the original order:

h e l l o

and reverse order

 o l l e h

and interleave them to form the final string

hoelllleoh

Some examples of the running program are shown below. User input is shown in bold.

$ python3 txtweave.py
Enter a phrase: cs21

Text weave is:
c1s22s1c

$ python3 txtweave.py
Enter a phrase: swarthmore

Text weave is:
sewraormthhtmroarwes

4. Text Grid

Write a program called txtgrid.py that makes a grid pattern out of strings using + and - symbols. Your program should prompt the user for the number of rows and columns in the grid and the width of all the columns. Each column should consist of width number of - symbols and adjacent columns should be seperated by a +. To design your program figure out how to build a single string that represents a single row in the grid. Then print that string out for the specified number of rows.

Some examples of the running program are shown below. User input is shown in bold.

$ python3 txtgrid.py
Enter number of columns: 3
Enter width of column: 2
Enter number of rows: 4

+--+--+--+
+--+--+--+
+--+--+--+
+--+--+--+

$ python3 txtgrid.py
Enter number of columns: 4
Enter width of column: 8
Enter number of rows: 5

+--------+--------+--------+--------+
+--------+--------+--------+--------+
+--------+--------+--------+--------+
+--------+--------+--------+--------+
+--------+--------+--------+--------+

5. Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-02.txt file in your cs21/labs/02 directory and answer the questions in that file.

Once you’re done with that, run handin21 again.

Turning in your labs…​.

Remember to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.