Why does this not work?

Sandeep Gupta sandeep182 at hotmail.com
Tue Jan 7 08:27:31 CET 2003


"Tetsuo" <member16943 at dbforums.com> wrote in message
news:2355358.1041918087 at dbforums.com...
> s = 0
> c = 0
> def halfadder(a, b):
>     if a and b in (0, 1):
>         if a and b == 1:
>             s = 1
>             c = 0
>             print s, c
>         elif a and b == 0:
>             s = 0
>             c = 0
>             print s, c
>         else:
>             s = 0
>             c = 1
>             print s, c
>     else:
>         print "Number out of range! Enter 0 or 1, please."
>
> halfadder(1,1)
> halfadder(1,0)
> halfadder(0,1)
> halfadder(0,0)

Your if statements need to be changed to:
if (a in (0,1)) and (b in (0.1))
if (a == 1) and (b == 1):
elif (a == 0) and (b == 0):
You may not need the parens, but I like it for clarity.
The problem is
elif a and b == 0:
gets parsed as
elif (a) and (b == 0):
so the condition is true if a is non-zero and b is zero.






More information about the Python-list mailing list