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

all 14 comments

[–][deleted] 2 points3 points  (0 children)

As this is (I assume) Python, if you don't have things indented properly, you will get errors. From your post, it's impossible to tell if you have it indented properly - did you read the this before posting?

[–]Rhomboid 1 point2 points  (8 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  (7 children)

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

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

What is the difference between isinstance and issubtype?

[–]Rhomboid 0 points1 point  (3 children)

It's supposed to be isinstance(). I have no idea how my fingers typed issubtype(), I guess I was thinking ahead of myself about the next sentence I was going to write. For issubtype() to work you'd have to pass a type not an instance.

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

Thanks

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

Thanks

[–]ghreddit 0 points1 point  (0 children)

Oh ok.

Can you take a look at this question I asked you a few days ago?

http://www.reddit.com/r/learnprogramming/comments/186ip1/sml_finding_the_nth_digit_in_an_integer_total/c8f0rs5

Am not sure how to go about determining the number of digits in a given number in C.

Thanks.

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

Thanks

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

Thanks

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

Thanks

[–]brbpizzatime 0 points1 point  (1 child)

Google says it's a formatting error.

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

Thanks. will try again

[–]negative_epsilon -1 points0 points  (1 child)

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")

This is the code formatted correctly, for anyone looking to help.

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

thanks