CS21 Lab 2: Numbers, Strings, and For Loops

Due Saturday, September 21, 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 have a top-level comment! Please see our function example page if you are confused about writing function comments.

Linux Filesystem Practice (Optional Warmup)

This is not a required part of the assignment, but we encourage you to try the Pokémon Scavenger Hunt to get some more practice using Linux terminal commands (cd, ls, pwd, cat).

If you solve the scavenger hunt, we will award you 1 extra credit point to replace one missed point over the course of the first five labs.

1. Making campaign streamers

Congratulations! You just got hired to help create advertisements for a political candidate! For your first job, your manager asks you to create streamers that you can hang from the ceiling. Each of your banners should have a border around it to serve as a nice outline.

Write a program called streamer.py that helps you design these streamers. You should ask the user for their favorite campaign slogan and then display the vertical streamer with a border of exclamation points around it.

1.1. Sample output

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

$ python3 streamer.py
What is your slogan? vote
!!!!!
! v !
! o !
! t !
! e !
!!!!!
$ python3 streamer.py
What is your slogan? let's go
!!!!!
! l !
! e !
! t !
! ' !
! s !
!   !
! g !
! o !
!!!!!

1.2. Requirements

Your program should meet the following requirements:

  1. Print the user’s slogan vertically.

  2. Print a boundary of exclamation points around the vertical slogan.

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. Making campaign banners

Your manager is pleased with all of the streamers that you’ve made and you’ve been promoted! You’re still in charge of making advertising, but now you’ve got to worry about advertising budgets too.

Your next task is to take a campaign slogan and repeat it a lot of times to make a giant banner that you can hang on the wall. Of course the campaign will want to use fancy ink to print the banner, but that ink costs $0.25 per character and you’ll want to tell your manager how much that banner will cost to make.

Write a program called banner.py that will ask the user for a slogan and the number of times they want to repeat it on the banner. Then, display the cost of the banner ($0.25 per character) and then display a mock-up of what the banner will look like.

You can assume that the number input by the user is a positive integer.

2.1. Sample output

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

$ python3 banner.py
What is your slogan? vote!
How often should it repeat? 3
Your banner will cost $3.75

vote!vote!vote!
$ python3 banner.py
What is your slogan? tacocat
How often should it repeat? 7
Your banner will cost $12.25

tacocattacocattacocattacocattacocattacocattacocat
$ python3 banner.py
What is your slogan? more CS labs please
How often should it repeat? 1
Your banner will cost $4.75
 
more CS labs please

2.2. Requirements

Your program should meet the following requirements:

  1. Print the total cost of the banner ($0.25 per character).

  2. Print the slogan the correct number of times using string multiplication or string accumulation.

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.

3. Voter registration

Great job on the advertising! Your manager is so impressed that they’ve decided to promote you into the field. You’re now in charge making sure that people are registered to vote.

Write a program called vote.py that will help you compute the total number of voters you’ve registered over a number of days. The program should ask the user for the number of days they worked and then, for each day, ask how many voters they registered that day. The program should then print out the total number of voters registered and the average number of voters registered per day.

3.1. Sample output

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

$ python3 vote.py
How many days did you work? 3

Day 1
How many voters did you register today? 4

Day 2
How many voters did you register today? 14

Day 3
How many voters did you register today? 7

You registered 25 new voters.
An average of 8.333333333333334 new voters per day.
$ python3 vote.py
How many days did you work? 2

Day 1
How many voters did you register today? 867

Day 2
How many voters did you register today? 530

You registered 1397 new voters.
An average of 698.5 new voters per day.
$ python3 vote.py
How many days did you work? 1

Day 1
How many voters did you register today? 1

You registered 1 new voters.
An average of 1.0 new voters per day.

3.2. Requirements

The code you submit for labs is expected to follow good style practices, and to meet one of the course standards, you’ll need to demonstrate good style on six or more of the lab assignments across the semester. To meet the good style expectations, you should:

  • Write a comment that describes the program as a whole using a comment at the top of the file.

  • All programs should have a main() function.

  • Use descriptive variable names.

  • Write code with readable line length, avoiding writing any lines of code that exceed 80 columns.

  • (Later labs) Add comments to explain complex code segments.

  • (Later labs) Write a comment for each function (except main) that describes its parameters, return value, and purpose.

Your program should meet the following requirements:

  1. Ask the user for the number of days the registration drive lasted and, for each day, collect the number of voters registered that day. Display the appropriate day number (e.g. Day 1, Day 2, etc.) before asking for the number of voters registered that day.

  2. Summarize the data by printing out the total number of voter registrations and the average number of voter registrations per day.

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.

3.3. Notes

  • You can assume that each value input by the user is a positive (non-zero) integer.

  • In the final example, notice that the output says "1 new voters" instead of "1 new voter". Don’t worry about that! We will learn later in the semester how to customize these messages to fix this.

  • As an extra challenge, you may format the average voters so that it only shows two decimal places, e.g. 8.33, instead of six, e.g. 8.333333.

4. Answer the Questionnaire

After each lab, please complete the short Google Forms questionnaire. Please select the right lab number (Lab 01) 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!