Skip to content

Homework 1: Statements

Objectives

  • Perform basic I/O using print() and input() functions.
  • Identify and correct PEP 8 style guide issues using flake8.

Docstring and PEP 8 checks

For full credit, your submissions must pass all formatting checks. Please read the Installing Thonny, flake8 and Docstring Requirements pages for more details.

Exercise 1.1 Casting Spells

Write a program named sorcery.py that prints the following quote from Interpretation of Computer Programs by Harold Abelson and Gerald Sussman.

We are about to study the idea of a computational process.
Computational processes are abstract beings that inhabit computers.
As they evolve, processes manipulate other abstract things called data.
The evolution of a process is directed by a pattern of rules called a program.
People create programs to direct processes.
In effect, we conjure the spirits of the computer with our spells.

Each line should be produced by a separate print statement in your code. Your output must match the above text exactly, character for character.

Exercise 1.2 Twelve Dozen

When used as a noun, a gross is a quantity equal to 12 dozen, or 144. Write a program named gross.py that inputs an integer from the user and prints out a number representing that many gross. For example, if the user enters 2, then the output should be 288.

The following shows an example of what the shell interaction should look like when your program is run twice in Thonny. In both cases the first number is typed entered by the user, and the second number is printed by the program.

>>> %Run gross.py 2 288 >>> %Run gross.py 100 14400

Be careful! Don't forget that input() will always return a string value. If you need an integer, you can use the int() function to perform the conversion.

x = input() # wrong!
x = int(input()) # correct!

Exercise 1.3 Kilroy Was Here

During World War II, the character of "Kilroy" was popular graffiti among American soldiers. 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). The fifth line begins with a tab and four spaces. There are no extra spaces at the end of any of the lines. To get full credit, you must reproduce the picture exactly, including special characters (tabs, quotes, slashes).

Exercise 1.4 Bits n' Bytes

A bit is a single binary digit, 0 or 1. It turns out that there are exactly \(2^d\) distinct sequences of \(d\) bits. For example \(2^3 = 8\), and there are exactly eight three-bit sequences:

000
001
010
011
100
101
110
111

A byte is a sequence of eight bits. For example the sequence 00100100 represents a single byte.

Write a program named bits_n_bytes.py that takes integer input from the user representing some number of bytes, then prints a message describing the corresponding number of bits and the number of distinct sequences of that length. For example, if the input is 2, the output is:

There are 16 bits in 2 bytes, with 65536 possible sequences.

In order to get credit for this question, your solution must use only one print statement.

Hint: You'll need to use the exponent operator: **. For example, we can calculate \(2^6\) (i.e., 2 to the 6th power) in Python as follows:

>>> 2 ** 6 64

Exercise 1.5 Say My Name

In Python, the multiplication operator * has the expected effect when applied to integers:

>>> 3 * 10 30

The * operator has a different meaning when a string is "multiplied" by an integer:

>>> '3' * 10 '3333333333'

In this case, multiplying by 10 resulted in 10 concatenated copies of the string '3'. As an aside, multiplying a string by a string would be an error:

>>> '3' * '10' Traceback (most recent call last): File "<pyshell>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str'

Write Python program named say_my_name.py that prompts the user for a name and number, and then prints the requested number of copies of that name. The prompts must be "Name: " and "Number: "

The following shows an example of what the shell interaction should look like when your program is run in Thonny:

>>> %Run say_my_name.py Name: Madison Number: 3 Your name is: MadisonMadisonMadison

In order to get credit for this question, your solution must use two print statements. Use one statement to print "Your name is: ", and use the other to print the repeated name.

HW1 Reflection (required)

Submit the graded survey on Canvas after completing the assignment.