0

i'm trying to make a calculator that can do 5 processes which are to add, subtract, multiply, divide and square root values that users input. all of that i've done, but when trying to make the values != float and int, the error comes for when the input values are float and int... i've tried value == str then error to come up, but even that doesnt work... anyone help me please?

e.g.

if value[0] != 'float' or value[0] != 'int' or value[1] != 'float' or value[1] != int':
    return 'the input value is not correct

i've tried both the two in the above and it does not seem to work. as in the program is not able to read between a float, int and a string.

if i input

calculator('+', 3, 4)

the result says the error.....

the reason why i have the 'print type(value[0], [1]) is to make sure the types they are for the error that keeps on occuring. `

def calculator(sym, *t):
    value = tuple(t)
    n = len(value)

    print type(value[0])
    print type(value[1])

    if value[0] != 'float' or value[0] != 'int' or value[1] != 'float' or value[1] != int':
        return 'the input value is not correct.'
    else:
        if sym == '+':
            if len(value) != 2:
                return 'The input value is not correct.'
            else:
                return float(value[0] + value[1])
        elif sym == '-':
            if len(value) != 2:
                return 'The input value is not correct.'
            else:
                return float(value[0] - value[1])
        elif sym == '/':
            if len(value) != 2:
                return 'The input value is not correct.'
            elif value[1] == 0:
                return 'The input value is not correct.'
            else:
                return float(value[0] / value[1])
        elif sym == '*':
            if len(value) != 2:
                return 'The input value is not correct.'
            else:
                return float(value[0] * value[1])
        elif sym == 'sqrt':
            if len(value) != 1:
                return 'The input value is not correct.'
            elif value[0] < 0:
                return 'The input value is not correct.'
            else:
                return value[0] ** 0.5
        else:
            return 'The input value is not correct.'

`

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
pheonixkid
  • 101
  • 1
  • 5
  • You're printing `type(value[0])`, but you compare against `value[0]`... – Bill Lynch May 07 '14 at 14:36
  • It doesn't matter much here, but please always copy and paste your code exactly. That can't be running code because of the syntax error in `or value[1] != int'`. – DSM May 07 '14 at 14:59

2 Answers2

4

Use isinstance to check type:

>>> isinstance(1, int)
True
>>> isinstance(1, float)
False
>>> isinstance('str', int)
False

You can use numbers.Number to match any kind of number object:

>>> import numbers
>>> isinstance(2, numbers.Number)
True
>>> isinstance(2.0, numbers.Number)
True
falsetru
  • 357,413
  • 63
  • 732
  • 636
1

You are trying to compare the values against strings, not types. Use the isinstance() function to test for types instead, it accepts a tuple of types:

if not (isinstance(value[0], (int, float)) and isinstance(value[1], (int, float))):

Note that float and int are names here, not string literals. They refer to the built-in type objects.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343