[Tutor] help with alternate execution

Dave Angel davea at ieee.org
Wed Sep 30 00:37:51 CEST 2009


wrobl1rt at cmich.edu wrote:
> I'm trying to make a very simple example to show alternate execution... if a 
> number is divisible by 3 it will say so and if it isnt, it will say so. Heres my 
> program---- 
>
> n= raw_input("enter a number= ")
> def divisible(n):
>     if n%3 == 0:
>         print n, "is divisible by 3"
>     else:
>         print n, "is not divisible by 3"
> print divisible
>
> when I try it out, and enter 3, I get this------
>
> enter a number= 3
> <function divisible at 0x02CC06B0>
>   
>
> I'm not sure why I am getting this and am not quite sure what this means... 
> could someone explain what I am doing wrong here and why it is telling me 
> this?
>
>   
First problem is that divisible is the function object.  If you actually 
want to call it, you need some parentheses, and in this case an 
argument.  I'd recommend you move all your top-level code to the end of 
the file, just so it's a bit clearer, though that's not your problem.  
With those two changes, we have:

def divisible(n):
    if n%3 == 0:
        print n, "is divisible by 3"
    else:
        print n, "is not divisible by 3"

n= raw_input("enter a number= ")
print divisible(n)

You're about to have another problem.  The error message is confusing, because the % operator has different meanings depending on the type of object it applies to.  To help you with it, I'll ask you what is the type of the object bound to n ?

You can find out by print repr(n), and I'd recommend you temporarily put that at the beginning of the function def.

DaveA




More information about the Tutor mailing list