我是编程的初学者。我有一个关于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
我还创建了一个类Andy,该类继承了People类,并删除了不相关的部分:
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
有人知道我的代码的哪一部分出错了吗?
答案 0 :(得分:1)
您的talk
函数需要返回值:
def talk(self):
talk = self.evidance(self.evidance_count)
self.evidance_count += 1
return talk
产生输出:
I heard something... someone saying...
I... I... oh yes! The killer is a guy!.
1
5
1