Skip to content

Homework 2: Expressions


  • Use arithmetic operators (+, -, *, /, **, //)
  • Use f-strings to format output with mixed types
  • Import and use functions from a built-in module

Exercise 2.1 Mad Libs

JMU's home page sometimes includes messages like "#2 public school in the South by U.S. News and World Report." It's fun to turn these messages into Mad Libs. Write a program named that begins with the following lines:

count = 149
noun = 'cats'
phrase = 'tap dance'

Then write a print statement that uses these variables to output the following message:

Over 149 students do research, take care of cats, or tap dance on campus.

Gradescope will change the values of count, noun, and phrase before running your program. Your output must correspond to the values chosen by Gradescope. For example, your program could be made to display this message instead:

Over 4321 JMU students do research, take care of a pile of rocks, or look for jobs on campus.

Exercise 2.2 Monograms

A monogram is a graphic symbol consisting of two or more letters combined (usually your initials), printed on stationery or embroidered on clothing.

The American Sweater Company of Iona Idaho (ASCII) makes sweaters with monograms. The management would like an easier way to determine a monogram from a customer's name. They have asked you to write a program, named, that will do the work for them.

The program should accept the customer's first, middle, and last names, and output the corresponding three letters. The input and output should look exactly like this:

First name? Alfred Middle name? Edward Last name? Neuman The customer Alfred Edward Neuman has ordered a sweater with the monogram AEN.

To get credit for this exercise, your code must use only one print statement. Notice there is a blank line between the input and the output.


The requirement for one print statement is for this exercise only. You may (and should) use multiple print statements on later exercises.

Exercise 2.3 Average Price

Your boss at Amalgamated Sales Inc. is wary of complex mathematical topics such as division. He asks you to create a program that will calculate the average price of an item given the cost for a number of them.

Write a program named that inputs the name of an item, the number of items purchased, and the total price paid per item (in that order). The program should then calculate the average price per item, store the average in a variable, and output the average formatted to two decimal places. Your output and input should look like this:

Enter the item name: banana Enter the number purchased: 100 Enter the total price: 153 The average price of a(n) banana is 1.53 dollars.

The prompts and output should match this example exactly, substituting the appropriate values. Note that you must calculate the average price yourself and format it to two decimal places. The name should be a string, the number purchased should be an integer, and the total price should be an integer number of dollars.

Exercise 2.4 Bits n' Bytes V2

In Exercise 1.4, you learned that \(d\) bits can form \(2^d\) sequences of 1's and 0's. But what about going the other way? For example, if we needed to represent 5 different sequences, how many bits would that take? Two bits aren't enough, since there are only four two-bit sequences:


It turns out that if we want to find the number of bits needed to have \(n\) different bit sequences, we can use the formula \(\lceil\log_2{n}\rceil\), which is read as "the ceiling of the log base 2 of n." The word "ceiling" means the closest integer greater than or equal to a number. Both ceiling and log are available in the math module.


The log function provided by the math module will default to base \(e\). You'll need to do some research to figure out how to calculate log base 2.

For this problem, write a program named which defines and assigns the following variables:

  • sequences - the number of bit sequences you want. Set this to 5. (Don't use the input function; assign the value directly.)
  • bits - the number of bits you would need, calculated according to the formula above.

Your program should not produce any output when executed. You can determine if it is working correctly by checking the "Variables" pane in Thonny. If the Variables pane isn't visible, you can select it under the "View" menu.

Exercise 2.5 Baking Cookies

Your family is baking cookies. Each member of the family gets the same number of cookies, as many as possible, and the extras go to Rocky, your pet raccoon. For example, if you bake 32 cookies, and there are 5 people in your family, each family member gets 6 cookies, and Rocky gets the 2 left over.

Write a program named that inputs the total number of cookies and the number of family members (excluding Rocky!), and outputs the number of cookies for each family member and for Rocky, according to the example below:

Enter the number of cookies: 32 Enter the number of family members: 5 Each family member gets 6 cookies, and Rocky gets 2 cookies.

HW2 Attribution (required)

Provide a short statement describing any assistance that you received on this assignment, either from another student, an online source, an AI-enabled tool, or any other source.

Submit your statement via Gradescope.