• Latest
  • Trending
How to Fix the “IndentationError” Error in Your Python Code

How to Fix the “IndentationError” Error in Your Python Code

March 23, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Friday, 17 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

How to Fix the “IndentationError” Error in Your Python Code

by ITECHNEWS
March 23, 2022
in Data Science, Leading Stories
0 0
0
How to Fix the “IndentationError” Error in Your Python Code

indentation is a vital feature of readable, maintainable code, but few languages enforce it. Python is one of those few.

If Python determines your code is indented incorrectly, you’ll be seeing the “IndentationError” message when you run your code. But how do you fix this, and how do you prevent it in the future?

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

Why Do You Get the IndentationError in Python?

The “IndentationError: expected an indented block” error is something you’re likely to see when you first start using Python, especially if you’ve come from another programming language.

The specifics of Python’s indentation rules are complex, but they boil down to one thing: indent code in blocks. This goes for functions, if clauses, and so on. Here’s an example of incorrectly formatted Python code:

fname = "Gaurav"
lname = "Siyal"

if fname == "Gaurav" and lname == "Siyal":
print("You're Gaurav")
else:
print("You're somebody else")

When you try to run the above code, you’ll get a message like this:

 File "tmp.py", line 5
 print("You're Gaurav")
 ^
IndentationError: expected an indented block

Instead, you should add either a tab or a series of spaces at the start of the two lines that represent blocks:

fname = "Gaurav"
lname = "Siyal"
 
if fname == "Gaurav" and lname == "Siyal":
 print("You're Gaurav")
else:
 print("You're somebody else")

If you indent with spaces, you can actually use any number you like, so long as you’re consistent and unambiguous. Most programmers use two, four, or eight spaces.

Common Cases of Correct Indentation

Here are some examples that you can refer to, so you can ensure you’re indenting correctly.

If statements

Indent the block that follows an if statement:

if my_name == "Gaurav":
 print("My name is Gaurav")
 return True

Functions

The body of a function is a block. You should indent this entire block:

def magic_number():
 result = 42
 return result
 
print magic_number()

For Loops

As with an if statement, the body of a for loop should be indented one level more than the line starting with the for keyword:

for i in range(10):
 print (i)

Make Sure Your Editor Indents Correctly

Most modern text editors support automatic code indentation. If your editor determines that a line of code should be indented, it will add tabs or spaces automatically.

In Spyder, indentation options are available under Tools > Preferences > Source code:

Spyder interface

If you’re using vim, you can edit your configuration and use the autoindent and related options to configure indentation. For example, here’s a common setup:

set autoindent
set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4

This will automatically indent using four spaces.

However, no editor can make automatic indentation bulletproof. You’ll still need to pay attention to indenting because some cases are ambiguous:

A Python function to compute the factorial of an integer

In this example, the final return statement is indented one level in from the function signature on the first line. However, if you position your cursor at the end of the penultimate line and press Enter, one of two things may happen. Your editor could position the cursor:

  1. Two indent levels in, aligned with “res =…”
  2. One indent level in, aligned with the “else:”

Your editor cannot distinguish between these two cases: you may want to add more code in the if/else block, or you may not.

Handling Python’s ‘Expected an Indented Block’ Error

Errors are an everyday occurrence in Python, just as in any other programming language. Python’s strict rules about indentation may add a new kind of error to think about, but they are useful. Properly indented code is more readable and consistent across teams.

Source: GAURAV SIYAL
Via: makeuseof
Tags: How to Fix the “IndentationError”
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version