Homework 1: Statements¶
Objectives¶
- Perform basic I/O using
print()
andinput()
functions. - Identify and correct PEP 8 style guide issues using
ruff
.
We are using two platforms
The first 20 pts are on Runestone. Look for the HW1.1 assignment on Runestone. Answer the questions (repeatedly if needed) until you get them all correct.
The next 30 pts are on Gradescope. There are three separate "assignments" – one for each exercise below (HW1.2, HW1.3, HW1.4). Resubmit each program until you get full credit.
Docstring and PEP 8 checks
For full credit, your Gradescope submissions must pass all formatting checks. Please read the Installing Thonny, etc. and Docstring Requirements pages for more details.
HW1.1 Concept Questions¶
Answer the following questions on Runestone:
- Activity: 1.3.3 Drag-N-Drop (intro-hardware-dnd-memory)
- Activity: 1.15.3 Fill in the Blank (intro-fitb-compilerVsInterpreter)
- Activity: 1.15.6 Multiple Choice (intro-MC-printVal)
- Activity: 1.15.11 Fill in the Blank (intro-SA-syntaxError_v2)
Note: You can browse to these questions manually or complete the "HW1.1" assignment on Runestone. Either way, you will receive credit for answering the questions.
HW1.2 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.
HW1.3 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!
HW1.4 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.
Reflection (required)¶
Submit the graded survey on Canvas after completing the assignment.