While reviewing a Python 3 script that inventories running services, you notice the author indented one line with a tab and the next with four spaces. Running the script immediately raises:
TabError: inconsistent use of tabs and spaces in indentation
The developer also wants to make the code conform to the PEP 8 style guide. Which modification will resolve the error and satisfy PEP 8 recommendations?
Replace every indentation level with exactly four space characters and remove all tabs.
Convert the file's line endings to LF (Unix style); inconsistent CRLF endings are what trigger the TabError.
Add a shebang of #!/usr/bin/env python -tt so Python automatically treats tabs as four spaces at runtime.
Replace every indentation level with a single tab character; PEP 8 requires tabs rather than spaces.
PEP 8 explicitly says to use four spaces per indentation level and warns that Python 3 forbids mixing tabs and spaces. Converting every indent to four ASCII spaces removes the mixed whitespace that triggers the TabError and matches the style guide. Switching to tabs alone might eliminate the runtime error, but it ignores PEP 8's preference for spaces. Changing line endings or adding the -tt option does not fix existing mixed indentation and therefore will not meet both goals.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
Why does Python 3 forbid mixing tabs and spaces for indentation?
Open an interactive chat with Bash
What is PEP 8, and why does it recommend using four spaces?
Open an interactive chat with Bash
How can you replace all tabs with four spaces in a Python file efficiently?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access