Chapter 2 Exercises

Part A: Concept Check (Short Answer)

A1. What does REPL stand for, and what does each letter mean in practice?

A2. Explain the difference between an interpreted language and a compiled language. Which is Python?

A3. What is the difference between the terminal and the shell?

A4. Why does Python care about indentation, while many other languages do not?

A5. What three pieces of information does every Python error message give you?


Part B: Predict the Output

For each code snippet, predict what Python will print before running it. Then run it to check.

B1.

print("CS", "is", "fun")

B2.

print("CS", "is", "fun", sep="-")

B3.

print("one", end=" ")
print("two", end=" ")
print("three")

B4.

print("*" * 20)
print("*" * 0)
print("ha" * 4)

B5.

name = input("Name: ")
print("Hello", name)
print(f"Hello {name}")

(Assume the user types Ada.)

B6.

print("Line 1")
print()
print("Line 3")

Part C: Fix the Bug

Each snippet contains an error. Identify the error type, explain why it happens, and fix it.

C1.

print("Welcome to Python!)

C2.

Print("Hello, World!")

C3.

message = "Hello"
print(mesage)

C4.

print("line one")
    print("line two")

C5.

print("Your age is: " + 25)

Part D: Code Writing

D1. Personal Info Card Write a program that asks the user for their name, age, and favorite food, then prints a formatted "info card" like this:

===== INFO CARD =====
Name: Ada
Age: 20
Favorite food: pizza
=====================

D2. Mad Libs Write a program that asks the user for a noun, a verb, an adjective, and an adverb, then prints a silly sentence using all four. Example output:

Enter a noun: elephant
Enter a verb: danced
Enter an adjective: purple
Enter an adverb: gracefully

The purple elephant gracefully danced across the stage!

D3. Receipt Generator Write a program that prints a formatted receipt. Hard-code the values (don't use input()). Include at least three items with prices, a subtotal line, and a total. Use string repetition for the divider lines.

D4. Custom Banner Write a program that asks the user for a message and then prints that message centered inside a box made of # characters. The box should be 40 characters wide. Example:

Enter your message: Hello World

########################################
#            Hello World               #
########################################

Hint: Look up the .center() string method, or calculate the spacing manually.

D5. Interview Bot Write a program that conducts a five-question "interview." Ask the user five different questions (your choice), store each answer in a variable, and then print a summary paragraph that weaves all five answers together. The summary should read like a natural paragraph, not a list.


Part M: Multiple Choice Practice

M1. What does python --version do? - (a) Installs Python - (b) Starts the REPL - (c) Displays the installed Python version number - (d) Updates Python to the latest version

Answer(c) Displays the installed Python version number.

M2. Which of the following is NOT a valid way to run a Python script? - (a) python myscript.py in the terminal - (b) Clicking the play button in VS Code - (c) Double-clicking the .py file in the file explorer (may work but not reliable) - (d) run myscript.py in the terminal

Answer(d) `run` is not a standard terminal command. The correct command is `python myscript.py`.

M3. What does input() return? - (a) An integer - (b) Whatever type the user types - (c) A string, always - (d) Nothing — it only displays a prompt

Answer(c) A string, always. Even if the user types a number, `input()` returns it as a string.

M4. What will print("ab" * 3) output? - (a) ab 3 - (b) ababab - (c) ab*3 - (d) An error

Answer(b) `ababab` — string repetition repeats the string the specified number of times.

M5. What's wrong with this comment? x = 5 # set x to 5 - (a) Nothing — it's fine - (b) Comments should use // not # - (c) It's not wrong syntactically, but it's a bad comment because it restates the obvious - (d) You can't put a comment on the same line as code

Answer(c) The comment just restates what the code already says. A better comment would explain *why* x is 5 (e.g., `# business days in a work week`).

Part E: Exploration & Extension

E1. REPL Explorer Open the REPL and try at least 10 expressions you haven't seen in this chapter. Document what you tried and what happened. Try to find at least one thing that surprises you. Write up your findings as comments in a .py file.

E2. Error Catalog Intentionally create at least five different Python errors (not just the three from the chapter). For each, record: (1) the code that caused it, (2) the full error message, and (3) how you fixed it. Save your catalog as a .py file with comments explaining each error.

E3. Terminal Mastery Using only the terminal (no graphical file manager), accomplish the following: 1. Create a folder called terminal-practice inside your cs1 folder. 2. Inside it, create three subfolders: project-a, project-b, project-c. 3. Create a file called notes.txt in project-a (hint: echo "some text" > notes.txt or touch notes.txt). 4. List the contents of terminal-practice to verify your structure. 5. Navigate back to your home directory in one command.

Document all the commands you used.

E4. Editor Customization Explore VS Code's settings. Find and change at least three settings (font size, theme, auto-save, etc.). Then install one additional VS Code extension that looks useful for Python development. Write a brief paragraph about what you found and changed.

E5. Language Comparison Look up the "Hello, World!" program in three other programming languages (e.g., Java, C, JavaScript, Rust, Go). Write a comparison: how many lines does each version take? What boilerplate is required? Why is Python's version simpler? Save your comparison as a commented .py file.