GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Sum of three given integers. However, if two values are equal sum will be zero - w3resource

Python: Sum of three given integers. However, if two values are equal sum will be zero

Last update on May 17 2025 05:43:49 (UTC/GMT +8 hours)


Triple Sum with Equality Rule

Write a Python program to sum three given integers. However, if two values are equal, the sum will be zero.

Pictorial Presentation:

Sum of three given integers. However, if two values are equal sum will be zero


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:

Flowchart: Sum of three given integers. However, if two values are equal sum will be zero.


For more Practice: Solve these Related Problems:

  • Write a Python program that returns the sum of three numbers unless all three numbers are equal, in which case return four times their sum.
  • Write a function that returns the sum of three numbers unless at least two numbers are equal, in which case return half their sum.
  • Write a script that checks if three numbers form an arithmetic sequence; if so, return their sum multiplied by 2.
  • Write a Python program that sums three numbers but subtracts the smallest number from the total before returning.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

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.

Python Code Editor:

 

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.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.