Homework #1

Objectives

  • Practice input and output using the input and print functions
  • Identify and correct PEP 8 style issues

Style Checks

For full credit, your submissions to these exercises must pass all flake8 style checks. If you haven’t done so already, you can install flake8 in Thonny by selecting Tools -> Manage Packages… and then finding and installing flake8 as well as pep8-naming. After installation, you should be able to check your file by typing the following into the Thonny shell:

>>> !flake8 hello_world.py

The exclamation point tells the Python interpreter the command should not be interpreted as Python code.

Exercise 1.1

Write a program named hello_world.py that displays the message Hello World! exactly as shown.

Once you are satisfied with your solution, submit hello_world.py through Gradescope.

Exercise 1.2 Wizardry

Write a program named spells.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 script. Your output must match the above text exactly, character for character.

Exercise 1.3 Gross

A gross is a quantity equal to 12 dozen, or 144. Write a program named gross.py that takes integer input from the user using the input function then 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 we need an integer, we can use the int() function to perform the conversion.

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

Exercise 1.4 Bits n’ Bytes

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

000
001
010
011
100
101
110
111

A byte is a sequence of eight binary digits. 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 as well as the total number of distinct sequences of that length. For example, if the input is 2 the output must be:

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

In order to get credit for this question, your solution may only use a single print statement.

Hint: In order to complete this exercise, you’ll need to use the Python exponentiation operator: **. For example, we can calculate 26 in Python as follows:

>>> 2**6
64

Exercise 1.5 Multiplying Strings

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

>>> 3 * 10
30

It 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 "", line 1, in
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: MadisonMadisonMadisonMadison

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

Back to Top