小弟在學笨方法學python ex41時遇到了點問題 誰能幫我下?

代碼如下:

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):
def__init__(self,***)":
"class %%% has-a__init__that takes self and *** parameters.",
"class %%%(object):
def ***(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 webside
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

"%answer
except EOFError:
print "
Bye"

然後程序不停的報錯File "ex41.py",line http://71.in &

question,answer = convert(snippet,phrase)

ValueError:need more than 1 value to unpack

求解啊


因為你的convert函數的results裡面只有一個值啊(不過就算有兩個我也不知道list能不能被展開成tuple


TZ你這態度不行啊,連改下縮進格式都不願意。

import random
from urllib.request import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%.",
"class %%%(object):
def __init__(self, ***)" :
"class %%% has-a __init__ that takes self and *** parameters.",
"class %%%(object):
def ***(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
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
else:
PHRASE_FIRST = False

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip().decode())

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 = list(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)

input("> ")
print("ANSWER: %s

" % answer)
except EOFError:
print("
Bye")


這個問題是不是應該舉報成不友善內容? python都不縮進實在是太不友善了!


不言自明,真坑啊~

(1)中文版(第3版)書上的代碼:

(2)網頁版 Learn Python The Hard Way上代碼:

不知道題主和我用的是不是一本書,如果是,明顯的,照著書上輸入肯定有問題,諸大神都說是縮進的問題,可如果書上代碼就有問題,難怪對報錯百思不得其解呢!

終於理解知乎大牛推薦閱讀原版英文教材了,如這坑一般,對我等代碼渣而言,一坑一個準啊~


這一節不是對著輸進去就可以了嘛....

不知道是不是因為看的pdf或者中翻教材...

建議上網頁版 Learn Python The Hard Way


網上PDF版的程序,正常縮進的,不過坑爹的是沒有下半段了。58行以後沒了!。。。什麼玩意兒。。。翻譯辛苦我們知道,不過翻成這樣還不如不翻。。。


推薦閱讀:

TAG:Python | 編程 | Python入門 | Python開發 |