CS21 Lab 2: Numbers and Strings

Due Saturday, February 27, before midnight (US/Eastern local to Swarthmore)

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. 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 apply the accumulator pattern to numeric and string types to solve problems

  • Learn to iterate over data using for loops

  • Practice string operations: +, *, len()

  • Practice using the range function

1. checkpoint

At the end of your lab session, be sure to run handin21.

Your lab instructor will check your files, which should reflect that you have made non-trivial progress (in python or pseudocode) towards the solution. Note that if you have not made much progress towards your solution, we expect that you would have been actively seeking help during your lab over Slack.

10% of your lab grade is dependent on this checkpoint.

If there are circumstances which prevent you from making substantial progress on this lab, please contact your lab instructor as soon as possible.

2. girl scout cookies

Every year, the Girls Scouts of America sell Girl Scout Cookies. Your neighbor, Olivia, is selling them for $4 a box and would like to know how much money she should have after a successful afternoon canvassing the neighborhood.

Write a program called cookies.py to keep track of how many cookies Olivia sells to her neighbors. Your program should allow Olivia to specify how many people she tried to sell cookies to and then, for each person, specify how many boxes of cookies they bought. After entering all the data, the program should show Olivia how many boxes she sold in total, how many boxes each neighbor bought on average, and how much total money she should have.

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

$ python3 cookies.py
How many houses did you visit? 5

Neighbor 1
How many boxes did they buy? 8

Neighbor 2
How many boxes did they buy? 2

Neighbor 3
How many boxes did they buy? 0

Neighbor 4
How many boxes did they buy? 1

Neighbor 5
How many boxes did they buy? 3

You visited 5 houses.
You sold 14 boxes of cookies.
Each house bought 2.8 boxes on average.
You should have 56 dollars.

$ python3 cookies.py
How many houses did you visit? 1

Neighbor 1
How many boxes did they buy? 1

You visited 1 houses.
You sold 1 boxes of cookies.
Each house bought 1.0 boxes on average.
You should have 4 dollars.

Your program should have the following features:

  • Ask Olivia for the number of houses she visited.

  • For each house, ask for the number of boxes of cookies she sold.

  • When all of the data is entered, print out the total number of boxes sold, the average number of boxes bought by each neighbor, and the total amount of money Olivia should have.

  • You can assume that each value input by the user will be a non-negative integer.

  • Don’t worry about writing "1 houses" or "1 boxes". We will learn later in the semester how to customize these messages to say "1 box" or "2 boxes".

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

3. lethargic

You bought, and then ate, a few too many cookies from Olivia and you’ve been feeling a little lethargic. In fact, it seems to have impacted pretty much everything you do, including how slowly you speak. For example, when you say "Hello" after eating 3 cookies, it comes out as "H…​e…​l…​l…​o…​", with 3 periods between each letter because you ate 3 cookies. The more cookies you eat, the more periods between each letter!

Write a program called lethargic.py that simulates what happens when you eat too many cookies. Ask the user to enter how many cookies they ate. Then, ask the user for a phrase they want to speak. Show the resulting output with the correct number of '.' characters inserted.

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

$ python3 lethargic.py
How many cookies did you eat? 3
What do you want to say? Nap?
N...a...p...?...

$ python3 lethargic.py
How many cookies did you eat? 5
What do you want to say? Mmm cookies
M.....m.....m..... .....c.....o.....o.....k.....i.....e.....s.....

Features:

  • The user can enter a string of any length.

  • Your output will insert periods equal to the number of cookies that they ate in between each character of the input string.

  • You can assume that the number of cookies input by the user will be a non-negative integer.

String multiplication (multiplying a string by a non-negative integer) results in the string being repeated by the integer you multiplied by.

Here are two example of string multiplication:

>>> 'hello' * 3
'hellohellohello'
>>> 'bye' * 0
''

4. rectangles and squares

4.1. rectangles

Write a program called rectangle.py that draws a rectangle on the screen using '@' characters. You will ask the user for the width and height of the rectangle and then draw the appropriate rectangle on the screen.

You can assume that the height and width are always integers greater than or equal to 2.

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

$ python3 rectangle.py
This program will draw a rectangle.
Width: 30
Height: 4
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@                            @
@                            @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


$ python3 rectangle.py
This program will draw a rectangle.
Width: 2
Height: 2
@@
@@

4.2. squares

Write a program called square.py that draws a square with a diagonal line going through it from left to right.

You can assume that the side length is always an integer greater than or equal to 2.

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

$ python3 square.py
This program will draw a square with a diagonal line.
Side length: 8
@@@@@@@@
@@     @
@ @    @
@  @   @
@   @  @
@    @ @
@     @@
@@@@@@@@

$ python3 square.py
This program will draw a square with a diagonal line.
Side length: 3
@@@
@@@
@@@

This question is surprisingly more challenging than the rectangle question above! Try drawing a picture of some example squares counting how many spaces there are before and after each '@' that makes up the diagonal.

For example, here’s a start using the side-length 8 square shown above:

@@@@@@@@
@@     @   <- @, 0 spaces, @, 5 spaces, @
@ @    @   <- @, 1 space,  @, 4 spaces, @
@  @   @   <- @, 2 spaces, @, 3 spaces, @
@   @  @      etc
@    @ @
@     @@
@@@@@@@@

Try it for other size squares and so you can work out the pattern of spaces and '@' characters you need in each row of the square and then use string concatenation to put all the pieces together.

5. answer the questionnaire

Each lab will have 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.

Once you’re done with that, run handin21 again.

6. x marks the spot — optional

This is an optional extra challenge. This part does not affect your grade so please only attempt this after completing the rest of your lab. It is simply an extra challenge, if you want to try it.

Write a new program, xmarks.py, that draws a pattern similar to the square you drew above except this time your program should draw both diagonals instead of just one.

Here are some examples:

$ python3 xmarks.py
Side length: 10
@@@@@@@@@@
@@      @@
@ @    @ @
@  @  @  @
@   @@   @
@   @@   @
@  @  @  @
@ @    @ @
@@      @@
@@@@@@@@@@

$ python3 xmarks.py
Side length: 9
@@@@@@@@@
@@     @@
@ @   @ @
@  @ @  @
@   @   @
@  @ @  @
@ @   @ @
@@     @@
@@@@@@@@@

Getting squares with even sides is easier than getting squares with odd sides. You might want to try assuming the user can only enter even numbers before trying to get odd numbers to work. Although you are learning about if statements this week, you can in fact solve this without an if statement. However, since it’s an extra challenge, you’re welcome to use if statements if you’d like!

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.