Reputation: 1
Return the sum of 3 numbers, but if they're the same, they dont count towards the total. Can you help me find what's wrong in my code please?
def lone_sum(a,b,c):
t=0
if a==b and a!=c:
t=a+c
elif a==c and a!=b:
t=a+b
elif b==a and b!=c:
t=b+c
elif b==c and b!=a:
t=b+a
elif c==a and c!=b:
t=a+b
elif c==b and c!=a:
t=b+a
elif a==b and b==c:
t=a
return t
Upvotes: 0
Views: 66
Reputation: 5648
You can achieve it within two lines of code by using set
like this
def lone_sum(a,b,c):
return sum(set([a,b,c]))
This much more efficient than all the above codes. I suggested this because i believe programming is meant to solve problems efficiently.
Upvotes: 1
Reputation: 61
def lone_sum(a,b,c):
if a==b and a!=c:
t=a+c
elif a==c and a!=b:
t=a+b
elif b==a and b!=c:
t=b+c
elif b==c and b!=a:
t=b+a
elif c==a and c!=b:
t=a+b
elif c==b and c!=a:
t=b+a
elif a==b and b==c:
t=a
else:
t = a + b + c
return t
print (lone_sum(21, 32, 32))
There's nothing wrong with your code, I think you forgot to call the function. I also added some small features to your code.
Upvotes: 0
Reputation: 2016
A simpler solution:
def lone_sum(a, b, c):
return sum({a, b, c})
Upvotes: 3