艰难地学习 Python Ex.41 对 For 循环感到困惑 [英] Learn Python the Hard Way Ex.41 Confused About For loop
问题描述
我无法理解 Learn Python the Hard Way ex.41 中 for 循环之一的工作原理.http://learnpythonthehardway.org/book/ex41.html 下面是课程中的代码.
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 the lesson.
我感到困惑的循环是 for i in range(0, snippet.count("@@@")):
它是否在 0 范围内迭代片段(其中有 6 个片段),并添加@@@"计数的额外值?那么对于下一行代码 param_count = random.randint(1,3)
是否应用了@@@"的额外值?还是我离题了!?
The loop that I am confused about is for i in range(0, snippet.count("@@@")):
Is it iterating over a range of 0 to snippet (of which there are 6 snippet), and adding the extra value of the count of "@@@"? So for the next line of code param_count = random.randint(1,3)
the extra value of "@@@" is applied? Or am I way off!?
干杯达伦
import random
from urllib import urlopen
import sys
WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []
PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%.",
"class %%%(object):\n\tdef __init__(self, ***)" :
"class %%% has-a __init__ that takes self and *** parameters.",
"class %%%(object):\n\tdef ***(self, @@@)":
"class %%% has-a function named *** that takes self and @@@ parameters.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
}
# do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
# load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = []
for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS, param_count)))
for sentence in snippet, phrase:
result = sentence[:]
# fake class names
for word in class_names:
result = result.replace("%%%", word, 1)
# fake other names
for word in other_names:
result = result.replace("***", word, 1)
# fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
# keep going until they hit CTRL-D
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"
推荐答案
snippet.count("@@@")
返回次数"@@@"
出现在 snippet
中.
snippet.count("@@@")
returns the number of times "@@@"
appears in snippet
.
如果"@@@"
出现6次,则for循环从0到6迭代.
If "@@@"
appears 6 times, then the for-loop iterates from 0 to 6.
这篇关于艰难地学习 Python Ex.41 对 For 循环感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!