[Tutor] need help with conditionals
Lie Ryan
lie.1296 at gmail.com
Sat Sep 26 04:20:50 CEST 2009
wrobl1rt at cmich.edu wrote:
> Hello all,
> I am still very new to Python and am working on conditionals. Im stuck on this
> problem and I'm sorry if this is something really simple that I'm missing but its
> been really frustrating me. Here's a copy of the problem, and sorry if its really
> long but its the end question about the logically equivalent expressions I'm
> stuck on. Anyways, heres a copy of the problem
>
it works for me...
"""
Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def truth_table(expression):
... print " p q %s" % expression
... length = len( " p q %s" % expression)
... print length*"="
... for p in True, False:
... for q in True, False:
... print "%-7s %-7s %-7s" % (p, q, eval(expression))
...
>>> truth_table("p or q")
p q p or q
=====================
True True True
True False True
False True True
False False False
>>> truth_table("not(p) and not(q)")
p q not(p) and not(q)
================================
True True False
True False False
False True False
False False True
>>> truth_table("not(p or q)")
p q not(p or q)
==========================
True True False
True False False
False True False
False False True
>>> truth_table("p and q")
p q p and q
======================
True True True
True False False
False True False
False False False
"""
How are you calling the truth_table()?
More information about the Tutor
mailing list