CS21 Lab 2: Numbers, Strings, and For Loops

Due Saturday, September 23, 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

Work through the following sections and ask if you have questions!

Programming Tips

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. Pumpkins for sale

Carving pumpkins into jack-o'-lanterns is a traditional Halloween activity. Halloween falls on October 31 and this time of year it is common to see pumpkins for sale at farmers' markets.

One local farmer wants you to help them figure out how much each pumpkin should sell for and calculate the total value of all of their pumpkins. Write a program called market.py that computes the value of each pumpkin the farmer has for sale that day and how much money they will sell for in total. The farmer plans to sell each pumpkin for $1.41 per pound.

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

$ python3 market.py
How many pumpkins are for sale? 3

pumpkin 1
How many pounds does this pumpkin weigh? 4
This pumpkin should sell for $5.64

pumpkin 2
How many pounds does this pumpkin weigh? 14
This pumpkin should sell for $19.74

pumpkin 3
How many pounds does this pumpkin weigh? 7
This pumpkin should sell for $9.87

The total value of all of your pumpkins is $35.25

You do not need to format the price so that it only shows two decimal places:

$ python3 market.py
How many pumpkins are for sale? 2

pumpkin 1
How many pounds does this pumpkin weigh? 10
This pumpkin should sell for $14.1

pumpkin 2
How many pounds does this pumpkin weigh? 6
This pumpkin should sell for $8.459999999999999

The total value of all of your pumpkins is $22.56

Sometimes the farmer has no pumpkins for sale!

$ python3 market.py
How many pumpkins are for sale? 0

The total value of all of your pumpkins is $0.0

2. In need of a nap

After buying a pumpkin at the farmers' market, you decided to make pumpkin pie. Your pie came out so well that you ate a lot of it and now that the sugar rush is over, you’ve been feeling a really tired. So tired that it is even impacting how slowly you speak! For example, when you say "Hello" after eating 3 slices of pie, it comes out as "H…​e…​l…​l…​o…​", with 3 periods between each letter because you ate 3 pieces of pie. The more pie you eat, the more periods between each letter.

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

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

$ python3 pumpkin.py
How many slices of pie did you eat? 5
What do you want to say? sleepy
s.....l.....e.....e.....p.....y.....

$ python3 pumpkin.py
How many slices of pie did you eat? 3
What do you want to say? mmm pumpkin pie
m...m...m... ...p...u...m...p...k...i...n... ...p...i...e...

$ python3 pumpkin.py
How many slices of pie did you eat? 0
What do you want to say? I'm not tired!
I'm not tired!
Use string multplication to generate the dots in between each letter.

After you recover from your sleepiness, you decide that you should make more pumpkin pies and sell them at the next farmers' market. To advertise your pies, you’re going to need a logo that will catch people’s attention. You haven’t settled on the words you want to put in your logo or the design, so you decided to write a few programs so you can try out your different ideas.

3.1. Horizontally in a square

Your first idea is straightforward: put your advertising phrase inside a rectangular box. Write a program called horizontal.py that asks the user to type in a phrase and then draws a rectangle around it using the + character.

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

$ python3 horizontal.py
Enter a phrase: Pie for sale!
+++++++++++++++++
+ Pie for sale! +
+++++++++++++++++

$ python3 horizontal.py
Enter a phrase: Garnet Pies
+++++++++++++++
+ Garnet Pies +
+++++++++++++++

3.2. Vertically in a square

Your second idea is to make the phrase go from top to bottom inside a rectangular box. Write a program called vertical.py that asks the user to type in a phrase and then draws a rectangle around it using the + character.

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

$ python3 vertical.py
Enter a phrase: Pie for sale!
+++++
+ P +
+ i +
+ e +
+   +
+ f +
+ o +
+ r +
+   +
+ s +
+ a +
+ l +
+ e +
+ ! +
+++++

$ python3 vertical.py
Enter a phrase: Garnet Pies
+++++
+ G +
+ a +
+ r +
+ n +
+ e +
+ t +
+   +
+ P +
+ i +
+ e +
+ s +
+++++

3.3. Diagonal words

Your final idea is to write the phrase diagonally, but this time without the box. Write a program called diagonal.py that asks the user to type in a phrase. Display the phrase diagonally, but don’t put your phrase inside a box.

$ python3 diagonal.py
Enter a phrase: Pie for sale!
P
 i
  e

    f
     o
      r

        s
         a
          l
           e
            !

3.4. OPTIONAL — Truth in advertising

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.

You decided to with truth in advertising. Since your pies make people sleepy 💤, you should make your logo in the shape of a Z. Write a program called zzz.py that asks the user for a phrase and writes it out in the shape of a Z…​ and put that Z inside a box!

$ python3 zzz.py
Enter a phrase: Garnet Pies
+++++++++++++++
+ Garnet Pies +
+           G +
+          a  +
+         r   +
+        n    +
+       e     +
+      t      +
+             +
+    P        +
+   i         +
+  e          +
+ s           +
+ Garnet Pies +
+++++++++++++++

Can you make your logo have three Z’s?

Enter a phrase: Pumpkin
+++++++++++++++++++++++++++
+ Pumpkin Pumpkin Pumpkin +
+       P       P       P +
+      u       u       u  +
+     m       m       m   +
+    p       p       p    +
+   k       k       k     +
+  i       i       i      +
+ n       n       n       +
+ Pumpkin Pumpkin Pumpkin +
+++++++++++++++++++++++++++

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, 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, like 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!