CS21 Lab 2: Numbers, Strings, and For Loops

Due Saturday, September 20, before midnight

Goals

The goals for this lab assignment are:

  • manipulate Python numeric and string data types

  • learn to iterate over data using for loops

  • practice string operations: +, *, len()

  • practice using the range function

  • practice using accumulators

Work through the following sections and ask if you have questions!

As you write programs, use good programming practices:

  • Use a comment at the top of the file to describe the purpose of the program (see example).

  • All programs should have a main() function (see example).

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

  • Avoid writing any lines of code that exceed 80 columns.

    • Always work in a terminal window that is 80 characters wide (resize it to be this wide)

    • In vscode, at the bottom right in the window, there is an indication of both the line and the column of the cursor.

Function Comments

All functions should include a comment explaining their purpose, parameters, return value, and describe any side effects. Please see our function example page if you are confused about writing function comments.

1. Box it!

Write a program called boxit.py that prompts the user for two things:

  1. A phrase

  2. A border character

You may assume that the user will input a single character when prompted for the border.

Given these inputs, the program should print the phrase vertically, one character at a time, with a border around all sides of it.

1.1. Sample output

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

Example 1
$ python3 boxit.py
Enter a phrase: hello there
Enter a border: $
$$$$$
$ h $
$ e $
$ l $
$ l $
$ o $
$   $
$ t $
$ h $
$ e $
$ r $
$ e $
$$$$$
Example 2
$ python3 boxit.py
Enter a phrase: CS 21
Enter a border: !
!!!!!
! C !
! S !
!   !
! 2 !
! 1 !
!!!!!

1.2. Requirements

Your program should meet the following requirements:

  1. Ask the user for a phrase and border, in that order.

  2. Vertically print the phrase with the selected border evenly spaced around it.

Your output should match the examples shown above when given the same inputs. Your solution should be contained within a main function that you call at the end of your program.

2. Calculating Sales Tax

Write a program called salestax.py that prompts the user for a number of items and then prompts for the cost of each item. After receiving all the user’s input, compute the subtotal (sum of the item costs) and a total with a 6% sales tax applied.

You may assume that the user will input a valid integer for the number of items and valid floats for the item costs.

2.1. Sample output

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

Example 1
$ python3 salestax.py
How many items are you buying? 3
Cost of item number 1? $2.00
Cost of item number 2? $9.00
Cost of item number 3? $4.00
Subtotal: $15.0
Total (6% tax included): $15.9
Example 2
$ python3 salestax.py
How many items are you buying? 2
Cost of item number 1? $5.50
Cost of item number 2? $2.99
Subtotal: $8.49
Total (6% tax included): $8.999400000000001

2.2. Requirements

Your program should meet the following requirements:

  1. Ask the user how many items they’re buying (an integer). Subsequently prompt the user that many times for the cost of each item (floats) as shown in the example above.

  2. Compute and print the subtotal (the sum of the item amounts) and the total (the subtotal with a 6% tax applied).

Your output should match the examples shown above when given the same inputs. Your solution should be contained within a main function that you call at the end of your program.

Output Formatting

Note that you are not expected to round the printed totals to the nearest cent. As shown in the examples above, it’s fine for you to print the result of converting a float to a string using the str(…​) function.

If you know how to do more complex string formatting, you’re welcome to use it. If you do, you may need to "escape" the percent sign as %% for it to appear in the way that you want.

3. Do you hear an echo?

Write a program called echo.py that prompts the user for two things:

  1. A word

  2. How "echoy" it is

You may assume that the user will answer "how echoy it is" with an integer.

Given these inputs, the program should print the chosen word and then repeat the word again the number of times corresponding to the "echoy" value. Between each echo, it should print an increasing number of period . characters.

3.1. Sample output

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

Example 1
$ python3 echo.py
Enter a word: echo
How echoy is it? 4
echo.echo..echo...echo....echo
Example 2
$ python3 echo.py
Enter a word: hello
How echoy is it? 2
hello.hello..hello
Example 3
$ python3 echo.py
Enter a word: Martin
How echoy is it? 5
Martin.Martin..Martin...Martin....Martin.....Martin
Example 4
$ python3 echo.py
Enter a word: test
How echoy is it? 0
test

3.2. Requirements

Your program should meet the following requirements:

  1. Prompt the user twice: first for a word and then for how "echoy" it is (integer) as shown in the examples above.

  2. Print the word followed by the number of echoes (repetitions of the word) horizontally on the same line.

  3. In between each echo, there should be an increasing number of period . characters.

Your output should match the examples shown above when given the same inputs. Your solution should be contained within a main function that you call at the end of your program.

4. Answer the Questionnaire

After each lab, please complete the short Google Forms questionnaire. Please select the right lab number (Lab 02) from the dropdown menu on the first question.

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

Submitting lab assignments

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.

Logging out

When you’re done working in the lab, you should log out of the computer you’re using.

First quit any applications you are running, including your vscode editor, the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) and choose "log out".

If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock xlock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!