programming logical functions: myand, myor and mynot

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Nov 16 20:23:38 CET 2001


"Gernot" <headroom02 at gmx.de> writes:

>Hi,

>I want to implement the functions myand, myor and mynot. They should have
>the same effect as the known boolean operators and, or and not.

>The trick is that I am not allowed to use the boolean operators and, or and
>not.

>This is what I tried:
>-----------
>x = raw_input("Type something (x): ")
>y = raw_input("Type something (y): ")

>def myand(x,y):
>    if bool(x) == 1:
>        if bool(y) == 1:
>         return 1
>    return 0

>if und(x,y) ==1:
>    print "true"
>else:
>    print "false"
>--------------

>I think, the idea is not that wrong, but anyway, it doesn't work! It has
>something to do with "bool (x)", I think. Maybe this is something like a
>module which i have to "activate" first??

>Please give me some hints, I would be very grateful

I think you've correctly worked out that AND is equivalent to the nested if
statements but it's not clear from the example what you're trying to do.
If you are in fact trying to evaluate input from the user you need to choose a
representation for true and false (such as 't' and 'f') and make explicit
tests for these.
If the input is just for testing and you're trying to understand how myand
works, then you just need to think of 'x' and 'y' as _expressions_, which will
be evaluated to true or false in the right context (I can't think of another
way of saying this as a hint rather than giving the answer outright).

Eddie



More information about the Python-list mailing list