Kaartik Malhotra
Kaartik Malhotra

Reputation: 21

How to use a random function inside an if statement and use speak function in python at the same time

I'm working on a Virtual Assitant using Python and a few libraries like speech_recognition, gtts, and mpg321.
Now my assistant has one specific reply for any identified dialog, but I want it to reply in a randomized fashion.

Here's the code

def violet(data):

    if "what is violet" in data:
        speak("Yours truly.")

    if "are you a robot" in data:
        speak("I don't know what you've heard but virtual assistants have feelings too!")

How to use the random function here?

Upvotes: 1

Views: 115

Answers (1)

Yunnosch
Yunnosch

Reputation: 26753

This should do the trick by using the random feature of choosing randomly from a sequence.

import random
# ...
speak(random.choice(["Yours truly.", "It is what you are talking to."]))

Upvotes: 1

Related Questions