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

I haven't read all of your code, but here's a suggestion - change 'running' to a boolean, it'll be cleaner that way.

Besides that, you may call random functions like so;

import random
# NOTE: Do NOT put open/close parens in this list.
my_funcs = [
   ord,
   chr,
   input,
   open
]
rand_func = random.choice(my_funcs)

# Execute
rand_func()
# or
random.choice(my_funcs)() 
# the latter assumes that no arguments are required

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!

I'm glad it does!

Please be aware, though, that statements do NOT count as functions! In Python 2.x, especially, 'print' is a statement, not a function (like 'import' and 'if') so don't cycle through those.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.