Reputation: 59
i'm a beginner in programming. I have a question about dictionary
, I have done some research but still doesn't fix the problem. I created a dictionary using integral 0, 1, 2 and 3 as key and (words)
as content. I tried using get()
function to retrieve the key in the dictionary and trying to print out the content in a if statement, but it prints out None. Below is the coding (not full but I take those related parts):
This is the dictionary:
class Fact(object):
facts = {
0 : "I heard something... someone saying...\nI... I... oh yes! The killer is a guy!.",
1 : "2",
2 : "3"
}
And this is how I code.
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
I also created a class Andy which inherits the class People, removed the unrelated part:
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)
This is the end part of my code to initiate the codes:
hint = 0
andy = Andy(1, 5, hint)
andy.play()
print andy.vital
print andy.mental
print andy.evidance_count
I didn't get an error. But this:
None
1
4
1
I was expecting to get this, :
I heard something... someone saying...\nI... I... oh yes! The killer is a guy!.
1
4
1
Does anyone know which part of my code has gone wrong??
Upvotes: 0
Views: 56
Reputation: 1116
Your talk
function needs to return the value:
def talk(self):
talk = self.evidance(self.evidance_count)
self.evidance_count += 1
return talk
Produces output:
I heard something... someone saying...
I... I... oh yes! The killer is a guy!.
1
5
1
Upvotes: 1