Ex41 stuck again

I’m looking at this code from ex41

    while True:
        snippets = list(PHRASES.keys()) #makes a list out of the key of the dictionary called snippet
        random.shuffle(snippets) #take the shuffle fucntion in random and uses snippet as apprameter. shuffles the snippets everytime the code runs

        for snippet in snippets:
            phrase = PHRASES[snippet]
            print(f'phrase: {phrase}')
            question, answer = convert(snippet, phrase) #Runs the convert function with snippet and phrase as parameters expecting question, answer as a return
            if PHRASE_FIRST: #If PHRASE_FIRST = TRUE (2 arguments where the 2nd argument is 'english') then switch around on answer and question 
                question, answer = answer, question


            input("< ")
            print(f"ANSWER: {answer}\n\n")

I can’t figure out what the below does, and have the feeling it holds the key for me to understand what going on.

phrase = PHRASES[snippet] 

I tried to run a print script just after hoping it’ll give me an idea, however for some reason nothing get printed in the shell.

You created a list of the dictionary keys and assigned to ‘snippets’.

snippets = list(PHRASES.keys())

Then for each key that is iterated over in the list of keys in the loop, which you’ve assigned to the variable ‘snippet’…

for snippet in snippets:

…you are saying go to the PHRASES and use the key assigned in that iteration as ‘snippet’ variable, assigning the resulting value back to ‘phrase’

phrase = PHRASES[snippet]

Or in other words, get the keys of the PHRASES values, make a list of them and call them ‘snippets’. Then (after then random shuffle), for each of the keys in the list, get the associated value and call it ‘phrase’.

With the ‘phrase’ variable now holding the actual value you want to display, you convert this into the question & answer variables so you can flip the order around in the IF statement that follows.

Its a common pattern to see singular items used with plurals in loops. The words themselves are user defined so can be anything, but it makes sense to think of singular version of a group.


for dinner in daily_meals:
for jigsaw_piece in puzzle:
for banana in fruit_bowl:
for chair in chairs:
for the_thing in the list_of_things:

Is just calling the value for the key specified (note the [] syntax for specifying the key):

phrase = PHRASES[dinner]
phrase = PHRASES[jigsaw_piece]
phrase = PHRASES[banana]
phrase = PHRASES[chair]
phrase = PHRASES[get the value associated to the key: 'the_thing' ]

HTH. Keep on trucking.

1 Like

Thank you @gpkesley

This was what I was looking for.
Basically that that phrase = PHRASES[ key ] where the key is the snippet that is derived from snippets = list(PHRASES.keys()). In other words

phrase is the key and snippet is the value in the in the PHRASES dictionary.

also thanks for the suggestion to future naming conventions.

1 Like

Anytime you see a line like that and you don’t know what it does, it’s commonly because you don’t know what everything on that line is. When I hit that line, the first thing I do is “what type is PHRASES and snippet”?

You find this by one of two ways:

  1. Finding where it was assigned most recently.
  2. Just print it out and Python will tell you what it is, or print(type(PHRASES)).

I also typically see that when a student says “I don’t know what this line does” what they really mean is “I can’t just look at this line and without running it know what it does.” I can’t even do that most of the time, and I just run it and figure it out that way. I can usually analyze the line or make a guess, but typically if I make my guess and find out that it’s wrong I then go back to actually running the code and finding out what it does.

That’s the other key to analyzing code: You cannot figure out what it really does by staring at it. You have to run it. In fact, this is ingrained in computer science and is a fundamental philosophical question in the field.

So if the greatest minds in all of computer science can’t solve this then you (and I) won’t either. Everyone has to run code to confirm that it works, figure out what it does, and fix it.

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