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:

Return true if the two given int values are equal or their sum or difference is 5

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:

Flowchart: Return true if the two given int values are equal or their sum or difference is 5.

Python Code Editor:

 

Previous: Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.
Next: Write a Python program to add two objects if both objects are an integer type.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.