我正在使用Learn Python the Hard Way来学习Python,当我运行以下代码作为示例时,我遇到'dict_keys' object does not support indexing error
:
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):\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
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())
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 = list(answer)
answer = list(question)
print(question)
input("> ")
print("ANSWER: %s\n\n" % answer)
except EOFError:
print("\nBye")
此代码适用于Python 2.x,我使用的是Python 3.5。我看到了以下帖子,但无法将解决方案转换为上述问题:TypeError: 'dict_keys' object does not support indexing。任何帮助表示赞赏。
答案 0 :(得分:1)
尝试替换此
bound_function = ChangeColour(boxes[1][2], boxes[5][2], main)
my_button = Button(root, bound_function)
get = my_button.pack(fill=BOTH)
用这个
snippets = PHRASES.keys()
正如您提供的链接所示,snippets = list(PHRASES.keys())
不会返回python 3中的列表。
答案 1 :(得分:0)
因此,您需要注意Python 2.X和Python 3.5.x之间存在一些差异。
来自@jprokbelly的答案将帮助您解决问题,但您还需要修改convert
函数中的代码,因为来自urlopen
的{{1}}将会返回字节流而不是字符串,这将导致错误消息:
urllib.request
因此,如果您想在Python 3.5.x中使用此代码,那么您至少需要做两件事。
TypeError: Can't convert 'bytes' object to str implicitly
更改为snippets = PHRASES.keys()
将snippets = list(PHRASES.keys()]
方法的开头更改为:
convert
您的另一个选择是设置您的开发环境以使用virtualenv并在Python 2.7.x中运行代码。
答案 2 :(得分:0)
这里有两个问题(在其他答案中提到),因为在Python 2和3中对字符串/字节类型的处理不同(3行代码受影响)和对象索引(1行受影响)。以下运行调整完成后(在4行代码中)完成:
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):\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
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())
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(str(WORDS), param_count)))
for sentence in snippet, phrase:
result = sentence[:]
# print(type(result))
# fake class names
for word in class_names:
# print(type(word))
result = result.replace("%%%", word.decode("utf-8"), 1)
# fake other names
for word in other_names:
result = result.replace("***", word.decode("utf-8"), 1)
# fake parameter lists
for word in param_names:
result = result.replace("@@@", word.decode("utf-8"), 1)
results.append(result)
return results
# keep going until they hit CTRL-D
try:
while True:
for i, item in enumerate(PHRASES.keys()):
print(i, "###", item)
snippets = list(PHRASES.keys())
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question = list(answer)
answer = list(question)
print(question)
input("> ")
print("ANSWER: %s\n\n" % answer)
except EOFError:
print("\nBye")