0

So i have a program called lonesum(a,b,c) that needs to add the 3 numbers entered together only if they are input once. so lonesum(1,2,3) would return 6 but lonesum(1,1,3) would return 3. I have that part. What i need help with is getting a statement to work that will return an error if the user enters anything that isn't an integer i.e. lonesum(.5,2,3) will return error. so far i have this while statement which i thought would work but doesn't,:

while (a,b,c) != int:
     print("Error")
     return None
while a==b==c:
    return 0
while a==b:
    return c
while b==c:
    return a
while a==c:
    return b
while a!=b!=c:
    sum1=(a+b+c)
    return sum1

(That is the rest of my code, that is the part that works)

My problem is the != doesn't work and I'm not sure what to do. Any suggestions?

  • 5
    What are you trying to achieve by using `(a,b,c) != int:`? What do you think this condition examines? – Dimitris Fasarakis Hilliard Nov 04 '16 at 20:07
  • the (a,b,c) is the input from when you run the lonesum(a,b,c) function. I was hoping that putting while (a,b,c), the input, != int, (an integer) that it would print error and return none. – Conor Hastings Nov 04 '16 at 20:11
  • 1
    Correct me if I'm wrong but isn't the print missing a double quote? – August Williams Nov 04 '16 at 20:12
  • 5
    A `while` that always returns on the first iteration is an `if` – Will Nov 04 '16 at 20:12
  • `(a,b,c)` is creating a tuple. And the comparison is just wrong, even something like `1 == int` will return `False` – UnholySheep Nov 04 '16 at 20:14
  • Possible duplicate of [Check if the number is integer](http://stackoverflow.com/questions/3476782/check-if-the-number-is-integer) – August Williams Nov 04 '16 at 20:15
  • i understand that now, but i entered it in that way so that i could better describe what i need to do. I need to have the first part return error if the user enters a non-integer. I am literally half way through my first course ever in python and computer science in general. So i don't know everything yet. – Conor Hastings Nov 04 '16 at 20:16

2 Answers2

3

Try something like this:

if not all(type(v) is int for v in (a, b, c)):
    print("Error!")
    return None

What you want to check is if each value in (a, b, c) is of integer type. So you must check each value, not the tuple itself.

The builtin function all iterates over something iterable and returns true if all of the values are true. I've created a generator expression that iterates over the tuple (a, b, c), comparing the type of each value to int. all iterates over that generator expression. So if all of the values in (a, b, c) have type int, it returns True.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • 1
    That works! thank you! If i can ask, do you think you could explain why it works so that i can understand it more? I'm still in the process of learning python and as nice as my professor is, she isn't very good at explaing things. – Conor Hastings Nov 04 '16 at 20:20
  • 1
    @ConorHastings: Read the docs for [`all`](https://docs.python.org/3/library/functions.html#all) and the [generator expression tutorial](https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions); that covers both of the components here. – ShadowRanger Nov 04 '16 at 20:25
  • 1
    Side-note: Specific type checking is usually a bad idea, since it makes your code not work with anything but `int`, even if it's logically integer data (e.g. `numpy` integer types, `gmpy2.mpz`, `long` on Python 2, etc.). That's why duck typing is encouraged, or failing that, checking a more generic abstract type with `isinstance`, e.g. in this case, `all(isinstance(v, numbers.Integral) for v in (a, b, c))`. – ShadowRanger Nov 04 '16 at 20:27
  • 1
    @ConorHastings: I applaud you for asking for a better explanation. It's encouraging that you're interested in learning, not just getting your assignment done. And it keeps me honest -- I got a bit lazy. – Fred Larson Nov 04 '16 at 20:31
  • You should probably also look at the Python Tutorial located at the main Python site, @ConorHastings. [Here's a link](https://docs.python.org/3/tutorial/index.html) for that. After that try looking through Q&A's here on StackOverflow, there's a **ton** of amazing content by great people. – Dimitris Fasarakis Hilliard Nov 04 '16 at 20:34
  • Thank you! I will be sure to check it out! – Conor Hastings Nov 04 '16 at 20:35
2

If you want the sum of all numbers that appear exactly once, there are more succinct ways to do it. Count them all, sum the ones that appear once:

from collections import Counter

def lonesum(a, b, c):
    counts = Counter((a, b, c))
    return sum(v for v, cnt in counts.items() if cnt == 1)

This implicitly raises an error if a non-numeric value is passed as an argument when it tries to sum them; if you want to print an error instead of propagating the exception, make it:

def lonesum(a, b, c):
    try:
        counts = Counter((a, b, c))
        return sum(v for v, cnt in counts.items() if cnt == 1)
    except TypeError:
        print("Error!")
        return None
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271