尝试从"以艰难的方式学习Python"来运行脚本。并得到这个错误,尝试了几件事,但我有点卡住了。在运行脚本
时,使用argv并且参数不足的人看到了类似的错误错误指向第70行:
question, answer = convert(snippet, phrase)
完整代码:
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, nd 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)
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"
答案 0 :(得分:1)
您正在尝试解包函数results
返回的convert(*args)
中的两个值。
但是,在您的函数convert
中,您将返回一个列有result
项的列表,该列表会附加到results
。所以,你只有一个值而不是两个来解包
为了完整起见....
假设您的函数convert
在此列表中返回了两个以上的值,您尝试解压缩它们。你也会收到一个错误,但这次你有太多的值要解压缩。在这种情况下,您可以采取以下措施:
some_var, *other = convert(snippet, phrase)
答案 1 :(得分:1)
以上注释对于您遇到的问题是正确的,但是发布的代码示例未显示正确的缩进。类中的for sentence in snippet, phrase
循环不会追加额外的值,从而产生正确的question, answer = convert(snippet, phrase)
。
将代码更改为以下代码,它应运行正常(在此处测试):
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)
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
答案 2 :(得分:-1)
<强>问题强>
在您的代码中,convert()返回一个列表,该列表是一个对象或值。通过执行question, answer = convert(snippet, phrase)
,您需要返回两个值,然后插入question
和answer
。
<强>解决方案强>
你应该改变
question, answer = convert(snippet, phrase)
到
results = convert(snippet, phrase)
然后,您可以通过
从结果中获得问题和答案(假设它们位于索引0和1)question = results[0]
answer = results[1]