ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Shivang Yadav, on March 19, 2021

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 for logical error

# 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:

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.

Python Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.