Q:

Python program to demonstrate logical errors

belongs to collection: Python basic programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 resultBut 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to print table of number entered by... >>
<< Print number with commas as thousands separators i...