﻿ Python: Return true if the two given integer values are equal or their sum or difference is 5 - w3resource

# Python: Return true if the two given integer values are equal or their sum or difference is 5

## Python Basic: Exercise-35 with Solution

Write a Python program that returns true if the two given integer values are equal or their sum or difference is 5.

Pictorial Presentation:

Sample Solution:

Python Code:

# Define a function 'test_number5' that takes two integer inputs: x and y.
def test_number5(x, y):
# Check if any of the following conditions are met:
# 1. x is equal to y.
# 2. The absolute difference between x and y is equal to 5.
# 3. The sum of x and y is equal to 5.
if x == y or abs(x - y) == 5 or (x + y) == 5:
# If any of the conditions are met, return True.
return True
else:
# If none of the conditions are met, return False.
return False

# Test the 'test_number5' function with different sets of input values and print the results.
print(test_number5(7, 2))
print(test_number5(3, 2))
print(test_number5(2, 2))
print(test_number5(7, 3))
print(test_number5(27, 53))

Sample Output:

True
True
True
False
False

Explanation:

The said code defines a function "test_number5(x, y)" that takes two integers "x" and "y" as arguments and check if they are equal, or if their sum or difference is equal to 5. If any of these conditions is true, the program would return "True", otherwise, it would return "False".

At the end, the function is called five times with different inputs and prints the outputs of the inputs.

Flowchart:

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿