Reading Code and the Terminal
A guided-inquiry activity. Work in a team of 3 to 4, or on your own. Read each Model, then answer the Critical Thinking Questions in order before revealing a hint.
Learning objectives
- Trace a short Python program and predict its output.
- Explain in your own words what a variable and a loop do.
- Read a command-line session and extract the key facts (who, what, result).
Prerequisites: none. This is a Day 1 starting activity.
Team roles
Model 1: Code and its output
agent = "Ada" tries = 3 print(agent, "has", tries, "tries left")
Output: Ada has 3 tries left
- Which two names in Model 1 store values, and what does each hold?
Check your thinking
agent holds the text Ada (a string); tries holds the number 3 (an integer).
- Why does the output show Ada and 3 instead of the words agent and tries?
Check your thinking
print shows the values stored in the variables, not the variable names.
- Predict the output if tries were changed to 1.
Check your thinking
Ada has 1 tries left (the program does not fix grammar; it just prints the value).
Model 2: A loop, traced
for n in range(1, 4):
print("step", n)
| pass | n | prints |
|---|---|---|
| 1 | 1 | step 1 |
| 2 | 2 | step 2 |
| 3 | 3 | step 3 |
- How many times does the loop body run, and why does it stop?
Check your thinking
Three times. range(1, 4) gives 1, 2, 3 and stops before 4.
- What changes on each pass through the loop?
Check your thinking
The value of n increases by 1 each pass.
- In one sentence, what does a loop let you avoid?
Check your thinking
Writing the same line many times by hand; the computer repeats it for you.
Model 3: Reading a terminal session
bob@lab:~$ whoami bob bob@lab:~$ cat readme.txt The next password is: rABbit2
- Who is logged in, and which command showed that?
Check your thinking
The user bob; the whoami command showed it.
- Which command displayed the file contents, and what fact did it reveal?
Check your thinking
cat readme.txt displayed the file; it revealed the next password, rABbit2.
- Reading carefully and extracting the key fact is a security skill. List the three facts a defender could record from this session.
Check your thinking
The user (bob), the actions taken (whoami, cat readme.txt), and the sensitive item exposed (a password in a readable file).
Do it now
Application
You are handed an unfamiliar log line: 09:06 admin login FAILED from 10.0.0.9. List three facts you can extract from it.
Reflection (process skills)
What helped your team agree on the output predictions? What is one thing you will do differently on the next Model?