我是编程的初学者。我有一个问题dictionary,我做了一些研究,但仍然没有解决问题。我使用整数 0、1、2 和 3 作为键和(words)内容创建了一个字典。我尝试使用get()函数来检索字典中的键并尝试在 if 语句中打印出内容,但它打印出 None。以下是编码(不完整,但我采用了那些相关部分):
这是字典:
class Fact(object):
facts = {
0 : "I heard something... someone saying...\nI... I... oh yes! The killer is a guy!.",
1 : "2",
2 : "3"
}
这就是我编码的方式。
class People(object):
def __init__(self, vital, mental, evidance_count):
self.vital = vital
self.mental = mental
self.evidance_count = evidance_count
def evidance(self, locate):
return Fact.facts.get(locate)
def talk(self):
talk = self.evidance(self.evidance_count)
self.evidance_count += 1
我还创建了一个继承类 People 的 Andy 类,删除了不相关的部分:
class Andy(People):
def play(self):
if self.mental < 6: #i only coded some basic print and raw_input before this part to reach my desired self.mental value = 4 which is less than 6.
print self.talk()
else:
print "You did't get any hint from Andy."
return Andy(self.vital, self.mental, self.evidance_count)
这是我启动代码的代码的最后部分:
hint = 0
andy = Andy(1, 5, hint)
andy.play()
print andy.vital
print andy.mental
print andy.evidance_count
我没有收到错误。但是这个:
None
1
4
1
我期待得到这个,:
I heard something... someone saying...\nI... I... oh yes! The killer is a guy!.
1
4
1
有谁知道我的代码的哪一部分出错了?