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?
Add a shebang of #!/usr/bin/env python -tt so Python automatically treats tabs as four spaces at runtime.
Convert the file's line endings to LF (Unix style); inconsistent CRLF endings are what trigger the TabError.
Replace every indentation level with a single tab character; PEP 8 requires tabs rather than spaces.
Replace every indentation level with exactly four space characters and remove all tabs.
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...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99
$19.99/mo
Billed monthly, Cancel any time.
3 Month Pass
$44.99
$14.99/mo
One time purchase of $44.99, Does not auto-renew.
MOST POPULAR
Annual Pass
$119.99
$9.99/mo
One time purchase of $119.99, Does not auto-renew.
BEST DEAL
Lifetime Pass
$189.99
One time purchase, Good for life.
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .