Python: Sum of three given integers. However, if two values are equal sum will be zero
Python Basic: Exercise-33 with Solution
Write a Python program to sum three given integers. However, if two values are equal, the sum will be zero.
Pictorial Presentation:
Sample Solution:
Python Code:
# Define a function 'sum_three' that takes three integer inputs: x, y, and z.
def sum_three(x, y, z):
# Check if any of the two input values are equal. If so, set 'sum' to 0.
if x == y or y == z or x == z:
sum = 0
else:
# If all three input values are distinct, calculate the sum of x, y, and z.
sum = x + y + z
# Return the calculated sum.
return sum
# Test the 'sum_three' function with different sets of input values and print the results.
print(sum_three(2, 1, 2))
print(sum_three(3, 2, 2))
print(sum_three(2, 2, 2))
print(sum_three(1, 2, 3))
Sample Output:
0 0 0 6
Explanation:
The said code defines a function "sum_three(x, y, z)" that takes three integers as its argument and returns their sum.
The function first checks if any of the three integers (x,y,z) are equal, if any two or three integers are equal, it assigns 0 to the variable "sum".
Otherwise, it assigns the sum of the three integers to the variable "sum".
Finally, it returns the value of "sum".
At the end, the function is called four times with different inputs and prints the sum of the inputs.
Flowchart:
Python Code Editor:
Previous: Write a Python program to get the least common multiple (LCM) of two positive integers.
Next: Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.
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://198.211.115.131/python-exercises/python-basic-exercise-33.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics