Learn Python the Hard Way Ex.41 Confused About For loop

后端 未结 2 1577
广开言路
广开言路 2020-12-21 16:46

I am having trouble understanding how one of the for loops works in Learn Python the Hard Way ex.41. http://learnpythonthehardway.org/book/ex41.html Below is the code from t

相关标签:
2条回答
  • 2020-12-21 17:36

    "try except" block runs the program until the user hits ^ D.

    "While True" loop inside "try" stores list of keys from PHRASES dictonary into snippets. The order of keys is different each time (because of shuffle method). "for loop" inside that "While loop" is to go through each snippet and call convert method on key and value of that snippet.

    All "convert method" does it to replace %%%, ***, and @@@ of that key and value with a random word from the url list of words and return a list (results) consists of two strings: one made from the key and one made from the value.

    Then the program prints one of the strings as a question, then gets user input (using raw_input("> ")), but no matter what the user entered, it prints the other returned string as the answer.

    Inside convert method, we have three different lists : class_names, other_names, and param_names. To make class_names, the program counts the number of %%% isnide that key (or value, but they are the same numbers of %%% in them anyways). class_names will be a random list of words in the size of the count of %%%.

    other_names is a random list of words again. How many words? in the number of *** found in key (or value, does not matter which one because it is the same in any pairs of them)

    param_names is a list of strings in the size of the number of @@@ found. Each string consists of one, two or three different words seperated by ,.

    'result' is a string. The program goes over the three lists (class_names, param_names and other_names), and replace something in result string with what it already made ready for it. Then append this into results list. The (for sentence in snippet, phrase:) loop runs two times because 'snippet' and 'phrase' are two different strings. So, 'result' string is being made two times (one for question one for answer).

    I put one part of this program to a smaller sub program to clarify how a list of a certain size from random words in the url is created:

    https://github.com/MahshidZ/python-recepies/blob/master/random_word_set.py

    Finally, I suggest to put print statements any where in code that you need to understand better. An an example, for this code I printed a number of variables to get exactly what is going on. This is a good way of debugging without a debugger: (look for the boolean variable DEBUG in my code)

    DEBUG = 1
    if DEBUG:
      print "snippet: " , snippet
      print "phrase: ", phrase
      print "class names: ", class_names
      print "other names: " , other_names
      print "param names: ", param_names
    
    0 讨论(0)
  • 2020-12-21 17:41

    snippet.count("@@@") returns the number of times "@@@" appears in snippet.

    If "@@@" appears 6 times, then the for-loop iterates from 0 to 6.

    0 讨论(0)
提交回复
热议问题