Hi all, I am a little wet behind the ears when it comes to programming. I am an old dog trying to learn new tricks and am making my way through LPTHW.
I am using 3.6.0. like it said to but I am stuck when it comes to the hilarious = False statement. I have Google searched this and they talk about other Booleans, etc, but it is as clear as mud with me.
Can someone explain this?
A boolean is a data type that can only be True or False. Its very useful for evaluation and all kinds of stuff. For example, you could code that ‘if something is true, then do something, but if it is false, do something else’.
In the example in the book, Zed is taking the variable named ‘hilarious’ and setting it with a value of False. He then uses that variable as the evaluation of his joke statement. So basically saying, ‘is this joke funny and stating no, its false, not funny’.
Whats being demonstrated here is that you can assign a true/false value to a variable, can call that variable’s result as an argument. In this case, when it prints out the sentence about whether the joke is funny, its prints “False” (rather than hilarious label).
Thanks, GP for the prompt response, however, I don’t think I explained it correctly.
I understand the workings but that function apparently does not work anymore.
Is there another workaround to do this function??
You can disregard if you want. I am going to buy Learn Python 2 The Hard Way after reading some more information about python and how python 3 is not really good for beginners such as myself.
Oh right, gotcha. You mean the whole piece on string formats (f-strings)?
To be honest, I’d stick with Python 3 as its its cleaning (IMO). I started with Python 2 and its not massively different but its all down to personal preference.
GP, my question is with regards to ‘True/False’. It has obviously been changed between P2 and P3 as that exercise works in P2.
I was just about to pay my money and order the full book as I am really enjoying it. Should I do it if some of the teachings are not valid, like in the sample PDF? Is there a work around, i.e. give another variable or make it ‘False’ and incorporate it in the print command so it prints it? That wasn’t the whole idea of boolean though.
Here is the same exercise in the P2 version of the book:
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
You can see that the False assignment is the same. Its the formatting (and inserting) of the variables into the strings that changed between P2 and P3. I much prefer P3 style as its more readable.
Silly question, are you sure you are not running this in P2 rather than P3?
Hey @Romulus, I don’t see what error you’re referring to. True and False both work the same in Python 2 and Python 3. Changing that between the versions would cause a massive break in the entire python world. Also the format function works in Python 3 too. So, can you explain with a paste of your error message why you think True and False doesn’t work?
@zedshaw, all I can do is apologise. After going through the exercises to Ex 20, I finally found your video’s. Smacked myself on the back of the head and went back through the exercises, read them all back to front, like you said, and found I had already ‘broken’ it.
With Ex 6, it was a simple ‘)’ error.
I taught myself ‘basic’ on a TRS-80 back in 1980, touched a bit on Unix while in the military and now it is all coming back.
I found that if all else fails, “Google It” works wonders.
There ya go. Now hopefully you’ll remember that error message and what it means. I’d take notes like, “When I see X error it’s probably a few lines up and a missing ).” That’ll help next time.
Also, keep in mind that I run the code in my books when I build the book, so if it’s broken like that then it’d show up in my book. If you get weird errors, first ALWAYS assume you messed something up. The world of coding is littered with the souls of programmers who thought their code was perfect and it must be the book/compiler/cpu/internet.