F.E. PPS Unit 1 Practical 4 solution

Practical 4: Program to Exhibit Indentation Errors

Problem Statement:

Write a Python program that exhibits indentation errors and demonstrates how they can occur.

Solution Code:

				
					# This code contains indentation errors for demonstration
 
def greet_user():
print("Hello, welcome to the Python program!")  # Missing indentation
 
def add_numbers(a, b):
result = a + b  # Missing indentation
	return result
 
x = 5
y = 10
 
print("Sum:", add_numbers(x, y))
greet_user()

				
			

Output:

IndentationError: expected an indented block after function definition on line 3

				
					print("Hello, welcome to the Python program!") # Missing indentation
IndentationError: expected an indented block after function definition on line 3
Process finished with exit code 1
				
			

Explanation:

Below is the explanation of F.E. PPS Unit 1 Practical 4 solution where we have written Python Program to Exhibit Indentation Errors.

  1. Indentation in Functions:
    • The function greet_user() contains a line print(“Hello, welcome to the Python program!”), which is not properly indented.
    • In Python, all code blocks inside functions need to be indented by at least one level (usually 4 spaces or a tab).
  2. Indentation in the add_numbers() function:
    • Inside the add_numbers() function, the line result = a + b is also not indented correctly.
    • The correct indentation would be to move result = a + b to the right, aligning it under the def add_numbers(a, b): line. The return result statement is correctly indented inside the function.
  3. Error Display:
    • If you try to run this program, Python will raise an IndentationError for both of the unindented lines. This is because Python uses indentation to define the scope of loops, functions, and conditional blocks, so proper indentation is critical for correct program execution.

Key Concept Learned:

  • Indentation Errors:
    • In Python, indentation is mandatory to indicate blocks of code.
    • Any inconsistency in indentation can lead to errors like IndentationError.
    • Python doesn’t use braces {} or other symbols to define code blocks; it uses indentation instead.
  • Understanding Code Structure:
    • How indentation controls the flow and structure of a Python program.
    • Importance of consistent and correct indentation when defining functions or other blocks of code.:
Tech Amplifier Final Logo