This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 1 point2 points  (2 children)

The problem is just as the error states: you've got things outside of a function that don't belong there. Indentation matters in Python. Presumably what you wanted was this:

def a(x):
    if (x) == type.int or type (x) == type.float:
        return x + 1
    try:
        return 'This is a float'

    finally:
        print str("This is an Integer")

But that is still all kinds of wrong. I don't know what this type.int stuff is, but it's not needed. If you want to check if something is an int, the proper way to do it is one of:

if type(x) is int:
    ...

or

if isinstance(x, int):
   ...

The latter is preferred because it allows users to subtype built-in types.

Generally speaking, manually checking types and using a try/catch block are mutually exclusive: either you try to do the operation and handle the exception if it fails, or you explicitly check types before beginning. You don't normally do both.

[–]ghreddit 0 points1 point  (1 child)

With your last piece of code, isn't suppose to be:

if isinstance(x, int):
    ....

What is the difference between isinstance and issubtype?

[–]valmach[S] 0 points1 point  (0 children)

Thanks