Python program to demonstrate logical errors

Here, we will learn about the logical errors in python with an example where we have user wrong operation on the variable. By Shivang Yadav Last updated : January 13, 2024

Python logical errors

Logical Errors in Python occur in the code when everything in the code is syntactically and semantically correct, but the desired output is missing because of some logical mistake done by the programmer.

These are very difficult to find as there is no error thrown by the compiler. And the programmer needs to go through the code to find and correct the error.

Some common types of mistakes by the programmer leading to logical errors are:

  • Wrong indentation of the code.
  • Solving the expression based on wrong operator precedence.
  • Using wrong variable name
  • Using the wrong operator to perform the operation.
  • Some type errors that lead to data loss in the program.

Python program to demonstrate logical errors

# Python program for logical errors

# Get input values from the user
num1 = int(input("Enter number 1 : "))
num2 = int(input("Enter number 2 : "))

# Performing addition operation 
sumValue = (num1 - num2)

# Printing the value
print("The sum of given numbers is ",sumValue)

Output

The output of the above program is:

Enter number 1 : 765
Enter number 2 : 23
The sum of given numbers is  742

Explanation

In the above code, we have declared two variables num1 and num2 which need to be added to return the result. But by mistake we have used - operator instead of +. This happens in large codes and you need to go through the whole module. The wrong operator leads to wrong output value.

To understand the above program, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.