0

I am writing a program where a user inputs a number in base 10 to be converted into that number in a different (or same) base.

def convert(a,b)

Where a is the input and b is the new base. I am instructed to have the program return the value "-1" if b is not an integer, if b assumes no value (ie blank), or if b is an integer that is less than 1.

I know how to convert the number, and I know how to have the program return -1 if b is less than 1, but how do I get the program to return -1 if b is empty or not an integer?

LChaos2
  • 167
  • 4
  • 1
    See http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python. – zoo May 21 '12 at 21:30

4 Answers4

14

Short answer: Don't.

Type checking is not Pythonic. Try doing what you want to do, and if it works, it works, if it doesn't, it throws an exception.

Likewise, returning -1 on failure is not very Pythonic, generally, you want to throw an exception on a failure. There are some (rare) cases where this makes sense, but in general it means that you have to check the value (rather than just catching the exception where needed) and makes for a harder to spot bug (the value is -1 rather than getting an exception).

Long answer: If you really need to, then you can use isinstance(value, int) to check if a value is an integer, however, this undermines Python's duck-typing, and is therefore a bad idea 99.9% of the time. It also doesn't necessarily work how you expect - for example, bool is a subclass of int, so isinstance(True, int) will return True - which may or may not be what you want.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • 4
    If you do have to use `isinstance`, it's generally better to check against [`numbers.Integral`](http://docs.python.org/library/numbers.html#numbers.Integral) rather than `int`. – Danica May 21 '12 at 22:38
1

This is one way that you can check b's value.

def convert(a, b=None):
    if isinstance(b, int) and b > 1:
        return do_convert(a, b)
    return -1
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
0

My suggestion is to take whatever you've got and try to turn it into a int. This is more Pythonic: it takes anything that can work like an integer, even if it is not, strictly speaking, of type int. At the same time, it assures that you only have to worry about int when you are writing your code.

def convert(n):
    try:
       n = int(n)
    except ValueError, TypeError:
       return -1
    # here, do the conversion and return the result
kindall
  • 178,883
  • 35
  • 278
  • 309
0

Looks like homework. Your teacher is not teaching you good programming practices.

Python programmers generally take a value as is. If possible the built-in python functions will convert the variables into the necessary type. If not possible it's up to the calling function to take some sort of action. This is called duck typing. If you know that the input will likely cause problems in the future you could add in some assert statements to prevent the bad input.

You can check if your variable is an integer with the isinstance() function. You can check if it's empty by checking if the variable is true or not.

If this wasn't a homework assignment, the best way to convert bases would be with the built in int(a, b) function.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51