CS21 Lab 2: Numbers and Strings

Due Saturday, February 9, before midnight

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   snowdepth.py     starbox.py
stretch.py         textladder.py   

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.
• 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: test, test again, and then test some more!
• 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.

Topics for this assignment

• Python numeric and string data types
• String operations: +, len(), []
• Accumulator pattern
• For loops

1. Snow depth

Write a program snowdepth.py that tracks snow depth over a sequence of days. Prompt the user for the initial snow depth in inches and the number of days to track. For each day ask the user how much snow has melted that day and how much new snow has fallen. Display the new snow depth at the end of each day.

Here is an example (user input in bold):

$python3 snowdepth.py Enter today's snow depth in inches: 3.2 Enter number of days to track: 2 Day 1: Amount of melting in inches: 1.2 Amount of new snow in inches: 4.5 New snow depth: 6.500000 inches Day 2: Amount of melting in inches: 2 Amount of new snow in inches: 0 New snow depth: 4.500000 inches Don't worry if the user enters input that causes the snow depth to be negative at the end of the day. We will learn how to validate input later in the semester. $ python3 snowdepth.py
Enter today's snow depth in inches: 0
Enter number of days to track: 2

Day 1:
Amount of melting in inches: 1.2
Amount of new snow in inches: 4.5
New snow depth: 3.300000 inches

Day 2:
Amount of melting in inches: 4
Amount of new snow in inches: 0
New snow depth: -0.700000 inches

Write a program called textladder.py that asks the user to enter some text and then displays the text vertically as a ladder as shown below. The left side of the ladder should display the original characters in the text from top to bottom, while the right side, displays the original characters reversed from bottom to top.

Here are two examples (user input in bold):

$python3 textladder.py Enter some text: ladder l--r a--e d--d d--d e--a r--l$ python3 textladder.py
Enter some text: yam

y--m
a--a
m--y

3. A Box of Stars

Write a program starbox.py that prompts the user for an integer n and then prints a rectangular box of * characters with n rows containing n stars each.

Here's a few quick examples (user input in bold):

$python3 starbox.py Enter box size: 3 *** *** ***$ python3 starbox.py
Enter box size: 10

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

4. Text Stretch

Write a program stretch.py that stretches a single word provided by the user by inserting dots between each letter. One dot should appear after the first, two dots after the second letter, and n dots after the nth letter.

Here are two examples (user input in bold):

$python3 stretch.py Enter a word: stretch Stretched word is: s.t..r...e....t.....c......h.......$ python3 stretch.py

Enter a word: cs21
Stretched word is:

c.s..2...1....

Note: you can use print() with no arguments to print a blank line.

Each lab has a short questionnaire at the end. Please edit the QUESTIONS-02.txt file in your cs21/labs/02 directory and answer the questions in that file.
Don't forget 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.