[Tutor] need help with conditionals

Alan Gauld alan.gauld at btinternet.com
Sat Sep 26 09:59:10 CEST 2009


<wrobl1rt at cmich.edu> wrote

> 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

First send us the real code you have used not a copy of your homework.

And secondly send us the complete error message not a summary.
Python error messages are extremely helpful once you learn how
to read them.

Then we might be able to help.

As Corey said my tutorial contains some stuff on understanding
booleans, but the most useful section for truth tables is in the
Functional Programming topic in the Advanced section.

But this question actually has nothing to do with conditionals
or booleans! :-)

> The following Python script prints out the truth table for the any 
> boolean
> expression in two variables: p and q:
>
> expression = raw_input("Enter a boolean expression in two variables, p 
> and q: ")
>
> 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))
> You will learn how this script works in later chapters. For now, you will 
> use it to
> learn about boolean expressions. Copy this program to a file named
> p_and_q.py,

Thats a terrible name BTW it would be better to call it truthtable.py!

>>>> from p_and_q import *

And this is bad practice!

What are you using for a tutorial?
If its not for a class I would consider finding another...

> Use the truth_table functions with the following boolean expressions, 
> recording
> the truth table produced each time:
>
> not(p or q)
> not(p and q)
> not(p) or not(q)
> not(p) and not(q)

Also show us a cut n pasdte of you running the program - so we can see
what you are typing as input. Using eval() like this is very prone to user 
error
and also considered bad practice!

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list