CS21 Lab 2: Numbers, Strings, and For Loops
Due Sunday, February 9 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.
1. Bar chart
Write a program called barchart.py
that prints a sample bar chart, with one bar per row. You will use a string of *
characters to represent the bar length. The first row should have one *
, and each additional row should have two more *
characters than the previous row. The program should ask the user for the number of rows in the bar chart and then print the chart. Each row should be numbered with the row number, starting at row 1. You are encouraged to use a string accumulator pattern to solve this problem.
1.1. Sample output
Two examples of the running program are shown below. User input is shown in bold.
$ python3 barchart.py Number of rows: 2 1: * 2: ***
$ python3 barchart.py Number of rows: 7 1: * 2: *** 3: ***** 4: ******* 5: ********* 6: *********** 7: *************
$ python3 barchart.py Number of rows: 11 1: * 2: *** 3: ***** 4: ******* 5: ********* 6: *********** 7: ************* 8: *************** 9: ***************** 10: ******************* 11: *********************
Don’t worry about row alignment if you have more than 10 rows. We will learn how to fix this later in the semsester.
1.2. Requirements
Your program should meet the following requirements:
-
Prompt the user for the number of rows in the bar chart.
-
Print a bar for each row
-
Print the correct amount of stars for each row, starting with one star and increasing by two stars for each row.
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. Ninjagrams!
Ninjagrams are going digital in CS21. You can send a ninjagram message to anyone you want. The cost is $0.15 per character, including spaces and punctuation. Write a program that will ask the user for how many ninjagrams they want to send and then prompts for each message to send.
Your program should print cost of each ninjagram and the total cost of all the ninjagrams at the end of the program. You do not need to worry about formatting the output of the cost to two decmal places.
You can assume that the number input by the user is a positive integer.
2.1. Sample output
Examples of the running program are shown below. User input is shown in bold.
$ python ninjagram.py Number of ninjagrams to send: 1 Enter message: NinjaGraam! This message will cost: $1.65 Total amount: $1.65
$ python ninjagram.py Number of ninjagrams to send: 3 Enter message: I love CS21! This message will cost: $1.7999999999999998 Enter message: Treat yo self This message will cost: $1.95 Enter message: Roses are red, violets are blue, let's have some fun and code up lab 2 This message will cost: $10.5 Total amount: $14.25
2.2. Requirements
Your program should meet the following requirements:
-
Ask the user for the number of ninjagrams they want to send.
-
Ask the user for the message to send for each ninjagram.
-
Print the cost of each ninjagram.
-
Print the total cost of all the ninjagrams.
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. Snow much fun!
Write a program snowamount.py
that keeps track of daily snowfall and snow depth in your location. Start by asking the user for the number of days they want to report. For each day, ask the user for the amount of snowfall and the amount of snow that melted that day. Display the snow depth at the end of each day and the total amount of snowfall over the period.
3.1. Sample output
Examples of the running program are shown below. User input is shown in bold.
$ python3 snowamount.py Enter number of days to report: 3 Enter new snow amount for day 1: 2.0 Enter amount of snow melt for day 1: 0 Snow depth at end of day 1: 2.0 Enter new snow amount for day 2: 1.2 Enter amount of snow melt for day 2: 0.3 Snow depth at end of day 2: 2.9000000000000004 Enter new snow amount for day 3: 0 Enter amount of snow melt for day 3: 2.0 Snow depth at end of day 3: 0.9000000000000004 Total snowfall: 3.2
$ python3 snowamount.py Enter number of days to report: 4 Enter new snow amount for day 1: 12.3 Enter amount of snow melt for day 1: 0 Snow depth at end of day 1: 12.3 Enter new snow amount for day 2: 18.7 Enter amount of snow melt for day 2: 0 Snow depth at end of day 2: 31.0 Enter new snow amount for day 3: 8.1 Enter amount of snow melt for day 3: 0 Snow depth at end of day 3: 39.1 Enter new snow amount for day 4: 2.1 Enter amount of snow melt for day 4: 1.0 Snow depth at end of day 4: 40.2 Total snowfall: 41.2
3.2. Requirements
Your program should meet the following requirements:
-
Ask the user for the number of days to record.
-
For each day, ask the user for the amount of snowfall and the amount of snow that melted that day.
-
Display the snow depth at the end of each day as the previous days amount, computed at the previous depth, plus the amount of new snow, minus the amount of snow that melted.
-
Summarize the data by printing out the total snowfall over the period.
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 non-negative float.
-
You may assume that the amount of daily melting is never more than the current snow depth.
3.4. General 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:
|
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 ( 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!