Foundations-of-Programming-Python practice question 4 of 10

What must be consistent within a Python code block for the code to run without syntax errors?

Full set: 60 questions
Answer options
A: Variable names
B: Indentation level Correct answer
C: Comment placement
D: Line length
Correct Answer:
B. Indentation level
Explanation

Python uses indentation to define the structure and grouping of statements into code blocks. Unlike other languages that use braces or keywords, Python requires that all statements within a single block must be indented at the same level. An IndentationError, a subtype of SyntaxError, is raised if the indentation is inconsistent, preventing the code from running. This strict requirement ensures code readability and unambiguous structure.

Why Incorrect

A. Inconsistent variable names cause logical or NameError issues at runtime, not syntax errors related to the code block's structure.
C. Comment placement is flexible and does not affect the execution flow or the definition of a code block.
D. Line length is a style guideline (PEP 8) for readability; exceeding recommended lengths does not cause a syntax error.

References

1. Python Software Foundation. (2023). The Python Language Reference (Version 3.12.0). Section 2.1.8: Indentation. Retrieved from https://docs.python.org/3/reference/lexicalanalysis.html#indentation

2. Python Software Foundation. (2023). The Python Tutorial (Version 3.12.0). Section 4.1: if Statements. Retrieved from https://docs.python.org/3/tutorial/controlflow.html#if-statements

3. Guttag, J. V. (2016). Introduction to Computation and Programming Using Python, With Application to Understanding Data (2nd ed.). MIT Press. Chapter 2.2: Branching Programs. (Discusses the role of indentation in structuring conditional blocks).

Scroll to Top