CS21 Lab 2: Numbers, Strings, and For Loops
Due Saturday, September 18, by 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 (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.
-
Atom shows the column number in the lower left and draws a vertical line down the editing window at 80 characters.
-
In
emacs
, at the bottom, center of the window, there is an indication of both the line and the column of the cursor.
-
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)
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
1. Building Excitement
Write a program excitement.py
that asks the user how excited they
are for the fall semester, then prints out a number of exclamations
equal to how excited they are.
$ python3 excitement.py How excited are you for the fall semester? 4 woo! wooo!! woooo!!! wooooo!!!!
Your program should have the following features:
-
The user selects the number of lines to print. You can assume the user enters a positive integer.
-
Each line should consist of a "woo" followed by a number of exclamation points.
-
The first line should contain one exclamation point; the second line two exclamation points, and so on.
-
Each line should contain one more
o
than!
.
2. Squares
Write a program called square.py
that asks the user for an area of a
square. The program should then (i) print out the side length for the
square, (ii) print out the perimeter of the square (the perimiter of
a square is the sum of the side lengths), and (iii) use $
symbols
to print out a text square. The length of the text square should be
the largest integer less than or equal to the actual side length of
the square.
Write a program called square.py
that asks the user for an area of a
square. The program should then
-
print out the side length for the square,
-
print out the perimeter of the square (the perimiter of a square is the sum of the side lengths), and
-
use
$
symbols to print out a text square. The length of the text square should be the largest integer less than or equal to the actual side length of the square.
You can assume the user enters a positive integer for the area.
Note: review lecture notes for week 2 for information about importing math functions.
Two examples of running the program are shown below. User input is shown in bold.
$ python3 square.py Enter the area of your square: 36 Your square has side length: 6.0. Your square has perimeter: 24.0. $$$$$$ $$$$$$ $$$$$$ $$$$$$ $$$$$$ $$$$$$ phoenix[temp]$ python3 square.py Enter the area of your square: 10 Your square has side length: 3.1622776601683795. Your square has perimeter: 12.649110640673518. $$$ $$$ $$$
3. Text Mountain
Write a program called mountain.py
that prompts the user for a word
or phrase, then prints out that prase as a text mountain, as follows:
$ python3 mountain.py Enter your text: cs21 c sss 22222 1111111 $ python3 mountain.py Enter your text: mountain m ooo uuuuu nnnnnnn ttttttttt aaaaaaaaaaa iiiiiiiiiiiii nnnnnnnnnnnnnnn
Note: This program could use some planning before you start! You will need to figure out how many copies of a letter to print in each line. Additionally, in order to center the mountain top, you’ll need to begin each line with a number of spaces. You might find this program easier if you practice incremental development.
The following steps aren’t required, but might be helpful to you as you write this program.
3.1. Text only
First, try to write a program that just focuses on the text that needs to get printed out each line, ignoring the number of spaces.
$ python3 mountain.py Enter your text: mountain m ooo uuuuu nnnnnnn ttttttttt aaaaaaaaaaa iiiiiiiiiiiii nnnnnnnnnnnnnnn
In this example, your program should print out a single 'm', then three 'o’s, then five 'u’s, etc. Identify the pattern in the number of copies of each letter, then use a for loop to print out exactly the right number of each letter in each line.
3.2. Centering the mountain.
Secondly, use your partial solution from the first step, and add in a number of spaces to "center" each line in the text mountain. You’ll need to know the entire string to understand how many spaces to add. In the example below, to center the mountain, the line with '1’s starts with zero additional spaces. The line with '2’s starts with one additional space, the line with 's' starts with two spaces, and the top of the mountain uses three.
$ python3 mountain.py Enter your text: cs21 c sss 22222 1111111
4. Voter Registration
A community organizer is running a registration drive and wants a
program to tally the number of voters registered over the course of
the drive. Write a program called vote.py
that
computes the total number of new registrations over a number of days
entered by the user.
Before exiting, the program should print out the total number of new
voters and the average number of new registrations per day.
Two examples of running the program are shown below. User input is shown in bold.
$ python3 vote.py How many days did you work? 4 Day 1 How many voters did you register today? 20 Day 2 How many voters did you register today? 6 Day 3 How many voters did you register today? 15 Day 4 How many voters did you register today? 10 You spent 4 days registering new voters. You registered 51 new voters. You registered 12.75 new voters per day. $ python3 vote.py How many days did you work? 1 Day 1 How many voters did you register today? 1000 You spent 1 days registering new voters. You registered 1000 new voters. You registered 1000.0 new voters per day.
Your program should have the following features:
-
Ask for the number of days the registration drive lasted.
-
For each day, ask for the number of voters registered that day.
-
When all of the data is entered, print out the total number of voter registrations and the average number of registrations per day.
-
You can assume that each value input by the user is a positive integer.
-
Don’t worry about writing "1 days". We will learn later in the semester how to customize these messages to say "1 day" or "2 days".
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.
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, like the browser and the terminal. Then click on the logout icon ( or ) 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 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!