简体   繁体   English

如何在if语句中使用随机函数并同时在python中使用speak函数

[英]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.我正在使用 Python 和一些库(如语音识别、gtts 和 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?这里如何使用随机函数?

This should do the trick by using the random feature of choosing randomly from a sequence.这应该通过使用从序列中random选择的random特征来解决问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM