Calling a random function
I've been doing some more work on my robot today and I'm having a bit of trouble with calling a random function like so:
import speech
import random
import pythoncom
running = 1
class OnTopic:
topic = "nothing"
def TopicChange(self):
self.topic = "nothing"
print OnTopic.topic
class Know_Raptors:
def RapYoung(self):
speech.say("Did you know raptors took good care of their young")
self.topic = "raptors young"
def RapFeathers(self):
speech.say("Did you know raptors probably had feathers")
self.topic = "raptors feathers"
AskRaptor = [ Know_Raptors().RapFeathers(), Know_Raptors().RapYoung() ]
def command_callback(phrase, listener):
if phrase == "tell me about raptors":
#Here's where I'm stuck.
random.choice(AskRaptor)
if phrase == "yes":
if self.topic == "raptors feathers":
speech.say("Yes. Fossils indicate some of them had feathers")
elif self.topic == "raptors young":
speech.say("Yes. Even cold-blooded predators can have a warm heart")
else:
speech.say("Yes what?")
listener1 = speech.listenfor(["tell me about raptors", "yes"], command_callback )
while running == 1:
pythoncom.PumpWaitingMessages()
By the way, this is using Michael gundlach's speech module (which is fabulous!)
What I want it to do is when I ask "tell me about raptors," I want my robot to randomly chose one of the two functions in the class Know_Raptors. The problem is when I ask, "tell me about raptors," it registers the phrase but nothing happens.
And besides that, I notice that instead of waiting for me to say "tell me about raptors" my robot goes and says both the functions in order.
I'd make the first line in the function just a string and then put the "speech.say" in the command callback for AskRaptor, but then I still want to assign the value "raptors young" or "raptors feathers" to "self.topic."
I've tried all sorts of things in the script and in the command prompt, but to no avail. Anyone know what I should do?
Thanks in advance!
Loren
Related Article: Random Function Error
is a Python discussion thread by wsn that has 1 reply and was last updated 5 years ago.
Seagull One
Junior Poster in Training
61 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Thanks vkumar42! Here's what the code looks like now. It works great!
import speech
import random
import pythoncom
running = True
class OnTopic:
topic = "nothing"
def TopicChange(self):
self.topic = "nothing"
print OnTopic.topic
def RapYoung():
speech.say("Did you know raptors took good care of their young")
OnTopic.topic = "raptors young"
print OnTopic.topic
def RapFeathers():
speech.say("Did you know raptors probably had feathers")
OnTopic.topic = "raptors feathers"
print OnTopic.topic
RapFunc = [ RapYoung, RapFeathers ]
def command_callback(phrase, listener):
if phrase == "tell me about raptors":
random.choice(RapFunc)()
if phrase == "yes":
if OnTopic.topic == "raptors feathers":
speech.say("Yes. Fossils indicate some of them had feathers")
elif OnTopic.topic == "raptors young":
speech.say("Yes. Even cold-blooded predators can have a warm heart")
else:
speech.say("Yes what?")
listener1 = speech.listenfor(["tell me about raptors", "yes"], command_callback )
while running == True:
pythoncom.PumpWaitingMessages()
Thanks again!
Seagull One
Junior Poster in Training
61 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Years Ago by
vkumar42