Objectives

  • Assign and use variables
  • Use and format floating-point numbers
  • Use the arithmetic operators + - * / ** and //
  • Create a module for use by other code

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 madlib.py 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 Kilroy

During World War II, the character of “Kilroy” was popular graffiti among American GIs. Write a program called kilroy.py which outputs the following picture of Kilroy:

             """"
            |.  .|
    ----///---()---\\\----

        Kilroy was here

The picture is five lines tall. The first line begins with a tab character (important!) and 9 spaces. The second line begins with a tab and 8 spaces. The third line begins with a tab. The fourth line is blank (no whitespace even). The fifth line begins with a tab and four spaces. There is no whitespace at the end of any of the lines. To get credit, you must reproduce the picture exactly, including whitespace and escape characters.

Exercise 2.3 Price Calculator

Your boss at Amalgamated Sales Inc. is wary of complex 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 average.py that inputs (from the keyboard) 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 and store it in a variable. Then it should output the average price.

Your output and input should look like this, for example:

>>> %Run average.py
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 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 your pet raccoon Rocky. For instance, 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 cookies.py 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:

>>> %Run cookies.py
Enter the number of cookies: 32
Enter the number of family members: 5

Each family member gets 6 cookies, and Rocky gets 2 cookies.

Exercise 2.5 Bits n’ Bytes Revisited

In problem 1.4, we learned that d bits can form 2d sequences of 1’s and 0’s. But what about going the other way? If we need to be able to represent 5 different sequences, for instance, how many bits will it take? Two bits isn’t enough, since there are only four two-bit sequences:

00
01
10
11

It turns out that if we want to find the number of bits needed to have different bit sequences, we can use the formula [log2n], which is read as the ceiling of the log base 2 of n. The word “ceiling” means the next integer bigger than a number. Both ceiling and log are available in the math module. (Be careful! 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 module named bits_n_bytes_2.py 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 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 this isn’t visible by default, you can select it under the “View” menu.

Back to Top