Re: [Tutor] need help with conditionals
On Sep 26, 2009, at 11:42 PM, Alan Gauld wrote: "Kent Johnson" wrote It appears to be http://openbookproject.net/thinkCSpy/ch04.html So it is, Thats a shame CSpy is one of my favourite "competitors" :-) Pity it's apparently encouraging the use of eval like this with no caveat. But to the OP, keep with it, its not a bad tutorial, shame about this exercise! Perhaps worth alerting the author? He's a nice guy and i e-mailed him many moons ago when i was working through that book with some concerns i had. He was really receptive and grateful to get feed back and was quick to make changes if he thought they would improv the text. A super good dude and it seems, a top notch educator. He tends to credit *everyone* who sends in a suggestion, no matter how minor. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with conditionals
On Sat, Sep 26, 2009 at 3:59 AM, Alan Gauld wrote: > What are you using for a tutorial? It appears to be http://openbookproject.net/thinkCSpy/ch04.html Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with conditionals
"Kent Johnson" wrote It appears to be http://openbookproject.net/thinkCSpy/ch04.html So it is, Thats a shame CSpy is one of my favourite "competitors" :-) Pity it's apparently encouraging the use of eval like this with no caveat. But to the OP, keep with it, its not a bad tutorial, shame about this exercise! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with conditionals
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/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with conditionals
wrobl...@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 = TrueTrueTrue TrueFalse True False TrueTrue False False False >>> truth_table("not(p) and not(q)") p q not(p) and not(q) TrueTrueFalse TrueFalse False False TrueFalse False False True >>> truth_table("not(p or q)") p q not(p or q) == TrueTrueFalse TrueFalse False False TrueFalse False False True >>> truth_table("p and q") p q p and q == TrueTrueTrue TrueFalse False False TrueFalse False False False """ How are you calling the truth_table()? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with conditionals
> 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 > > --- > > >>> from p_and_q import * > >>> truth_table("p or q") Are you calling the function as shown above? > > Whenever I try to enter the expressions listed into the prompt it tells me the > string object is not callable It would be helpful if you showed us exactly what you type into the prompt and the exact error you get. You should be calling the function as it is shown in the problem. Assuming the function is entered and imported correctly you should get a true table output. two examples: >>> truth_table("p or q") p q p or q = TrueTrueTrue TrueFalse True False TrueTrue False False False >>> truth_table("not(p or q)") p q not(p or q) == TrueTrueFalse TrueFalse False False TrueFalse False False True Without a bit more info its hard to guess what is happening. Chris___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] need help with conditionals
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 --- To better understand boolean expressions, it is helpful to construct truth tables. Two boolean expressions are logically equivalent if and only if they have the same truth table. 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, then run it from the command line and give it: p or q, when prompted for a boolean expression. You should get the following output: p q p or q = TrueTrueTrue TrueFalse True False TrueTrue False False False Now that we see how it works, let’s wrap it in a function to make it easier to use: 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)) We can import it into a Python shell and call truth_table with a string containing our boolean expression in p and q as an argument: >>> from p_and_q import * >>> truth_table("p or q") p q p or q = TrueTrueTrue TrueFalse True False TrueTrue False False False >>> Use the truth_table functions with the following boolean expressions, recording the truth table produced each time: not(p or q) p and q not(p and q) not(p) or not(q) not(p) and not(q) Which of these are logically equivalent? - Whenever I try to enter the expressions listed into the prompt it tells me the string object is not callable ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor