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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/python-basic-exercise-35.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics