CS21 Lab 2: Numbers, Strings, and For Loops

Due Sunday, 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. Boxing Letters (Vertical)

Write a program called boxing-vertical.py that prompts the user to enter a phrase and then prints the phrase back, one character per line, with each character having a box (brackets) around it.

1.1. Sample output

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

Example 1
$ python3 boxing-vertical.py
Enter a phrase: Hello!
[H]
[e]
[l]
[l]
[o]
[!]
Example 2
$ python3 boxing-vertical.py
Enter a phrase: test message
[t]
[e]
[s]
[t]
[ ]
[m]
[e]
[s]
[s]
[a]
[g]
[e]

1.2. Requirements

Your program should meet the following requirements:

  1. Prompt the user for a phrase.

  2. Print each character in the phrase, one character per line, with each character in between a pair of square brackets ([ and ]).

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. Exercise logging

Write a program called exercise.py that first prompts the user for the number of days that they worked out. For each day, the program should prompt for how many minutes the user exercised that day. After all the days are entered, it should print the total number of minutes and the average number of minutes per day.

You may assume that the user will input valid, non-negative integers when prompted for the number of days and minutes exercised.

2.1. Sample output

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

Example 1
$ python3 exercise.py
How many days did you exercise? 4

How many minutes on day 1? 30

How many minutes on day 2? 25

How many minutes on day 3? 10

How many minutes on day 4? 15

After 4 days...
You exercised 80 minutes.
That's an average of 20.0 minutes per day!
Example 2
$ python3 exercise.py
How many days did you exercise? 3

How many minutes on day 1? 10

How many minutes on day 2? 25

How many minutes on day 3? 15

After 3 days...
You exercised 50 minutes.
That's an average of 16.666666666666668 minutes per day!

2.2. Requirements

Your program should meet the following requirements:

  1. It prompts the user for a number of days.

  2. For the number of days specified, prompt the user for a number of minutes, where the prompt contains the day number counting up from 1 on the first day.

  3. After minutes for each day are entered, it prints the total number of minutes.

  4. Finally, it prints the average number of minutes per day.

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

You do NOT need to worry about rounding fractional values in the final average. It’s fine to have a long repeating decimal like the one in the example above.

To print a blank line, you can call print() with no arguments.

3. Banners

2026 is an election year, so let’s write a program called banner.py to help make some banners. This program will prompt the user for two things: a slogan and a number of times to repeat. You’ll use this information to produce a banner and then compute the cost of printing it (at a price of $0.32 per character).

You may assume that the user will input a valid, non-negative integer when prompted for the number of times to repeat.

You might find some of the string operations that we talked about in class helpful for solving this problem.

3.1. Sample output

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

Example 1
$ python3 banner.py
What will your banner say? GoVote
How many times should it repeat? 3

GoVoteGoVoteGoVote
Your banner costs: $5.76
Example 2
$ python3 banner.py
What will your banner say? Again and
How many times should it repeat? 7

Again andAgain andAgain andAgain andAgain andAgain andAgain and
Your banner costs: $20.16
Example 3
$ python3 banner.py
What will your banner say? CS21 is the best!
How many times should it repeat? 1

CS21 is the best!
Your banner costs: $5.44

3.2. Requirements

You do NOT need to worry about rounding fractional values in the final cost. It’s fine if your program outputs a long sub-penny decimal result.

Your program should meet the following requirements:

  1. It prompts the user with a phrase for the banner and the number of times it should repeat.

  2. It prints the banner with the phrase repeated the number of times specified by the user.

  3. It prints the total cost of the repeated banner, with a charge of $0.32 per character.

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. Boxing (horizontal this time)

Write a program called boxing-horizontal.py that prompts the user for three things:

  1. A phrase

  2. Left symbol(s)

  3. Right symbol(s)

Given these inputs, the program should print the phrase all on one line (horizontally) with the left symbols appearing before each character in the phrase and the right symbols appearing after each character.

4.1. Sample output

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

Example 1
$ python3 boxing-horizontal.py
Enter a phrase: Hello!
Enter left symbol(s): [
Enter right symbol(s): ]
[H][e][l][l][o][!]
Example 2
$ python3 boxing-horizontal.py
Enter a phrase: test message
Enter left symbol(s): ((
Enter right symbol(s): ))
((t))((e))((s))((t))(( ))((m))((e))((s))((s))((a))((g))((e))
Example 3
$ python3 boxing-horizontal.py
Enter a phrase: CS is fun
Enter left symbol(s): 1
Enter right symbol(s): 2
1C21S21 21i21s21 21f21u21n2

4.2. Requirements

Your program should meet the following requirements:

  1. Prompt the user three times: first for a phrase and then for each of the left and right symbol(s) as shown in the examples above.

  2. Print the phrase on one line of output, with…​

    1. Each character having the left symbol(s) before it.

    2. Each character having the right symbol(s) after 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.

5. 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) 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!