Python 2.7 值错误:解包需要超过0个值(艰难学习Python第41章)
我看了所有关于解包错误和LPTHW第41题的回答,还花了一个小时检查我的代码,但我还是搞不懂我的错误在哪里。
以下是我的代码:
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 #ERROR : after print - weird
except EOFError:
print "\nBye"
具体的错误信息是:
Value Error: Need more than 0 vales to unpack
我刚开始学习,所以如果能解释一下这个错误为什么会发生,以及怎么修复它,我会非常感激。
3 个回答
0
在你的 convert
函数里,你多次修改了 result
,但是没有把它加到 results
列表里。你在最后一次循环中做对了,但可能需要在之前的循环中也加上:
for sentence in snippet, phrase:
result = sentence[:]
results.append(result) # added this
# fake class names
for word in class_names:
result = result.replace("###", word, 1)
results.append(result) # added this
# fake other names
for word in other_names:
result = result.replace("***", word, 1)
results.append(result) # added this
# fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result) # this one was already here
否则,results
就会一直是一个空列表,这样 convert
返回的就是 []
。这样在这里就无法正确拆分:
question, answer = convert(snippet, phrase)
因为你需要的正好是两个元素:问题和答案。
1
你的代码有缩进问题,三个 for
循环必须放在第一个循环里面,也就是要嵌套在一起。
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)
2
你的代码基本上是对的,只是最后三个循环的缩进有问题,它们应该是嵌套在一起的。
把下面的代码缩进4个空格,你就可以正常运行了:
# 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)