Ex43 how to get past more difficult rooms?

#1

Hi there,

I am working on ex43 from the book LPTHW… In the study drill of ex43, there’s a question that asks us to add some codes to get past more difficult rooms. The author could do it with two words, which honestly kinda blow my mind off xd. Does anyone know how to do this? I’d really appreciate it for any help!

xx

#2

Maaaaybe, can you reply with a direct quote so that I know what you’re exactly talking about?

#3

yeah sure. so the direct quote goes: 4. Add cheat codes to the game so you can get past the more difficult rooms. I can do this with two words on one line.

Shaw, Zed A… Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code (Zed Shaw’s Hard Way Series) (p. 177). Pearson Education. Kindle Edition.

#4

Ok, let’s see if this little session helps you figure it out:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> i = False
>>> 
>>> worked = x or i
>>> worked
1
>>> worked = i or x
>>> worked
1
>>> i = True
>>> worked = i or x
>>> worked
True
>>> 
>>> code = '1234'
>>> guess = 'cheater'
>>> 
>>> code == guess or 'cheater'
'cheater'
>>> code
'1234'
>>> 
>>> # another way
... guess == code or guess == 'cheater'
True
>>> 
3 Likes
#5

That’s brilliant! Thanks a lot Zed! Btw, it seems that you are working on a new booking on Unix, which I am really looking forward to reading as I just found myself very interested in linux. I’m wondering when approximately would this book be published?

#6

Hi Zed,

Sorry…I am not sure I understood. I also do not manage to see the whole picture/the mechanism behind the code that adds cheat codes.
I wrote the code in Python Shell and I played with it a bit. I understand the mechanism of “OR” (i.e., that as long as one of the terms is true, the result is true), but I still do not get the general picture.

Could you please explain briefly?

Thank you!

All the best!

#7

It’s pretty easy but you should play with it like I did in the Python shell (just type python and you can type snippets of python to test them).

The logical operators or, and and don’t just return True or False, they return the values you put into them. So if you do “my_var = X or Y” it’s literally going to set my_var to whichever (X or Y) was “truthy”. If Y is None and X is “Howdy”, the my_var will be “Howdy”. If X is None but Y is “Pardner”, then my_var will be set to “Pardner”.

Play with it. That’s the easiest way to see what’s going on. Also, try out:

x = y and j or z

With different combinations.

1 Like
#8

This code:

x = y and j or z

was hard for my brain, but I did it. Thank you! I understood now both codes.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.