LPTHW Exercise 6 - Strings and Text

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?

https://learnpythonthehardway.org/python3/ex6.html

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).

1 Like

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.

https://docs.python.org/3/library/constants.html

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.

https://learnpythonthehardway.org/book/nopython3.html

Thanks

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 x
print y

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.

1 Like

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.