首页 > 解决方案 > 虚拟助手代码跳过 if 语句

问题描述

我已经开始研究我自己的虚拟助手了。几天前这很好用,但我相信我搞砸了。它应该找出我输入的命令然后响应,但它总是说“问候”-响应。

from time import ctime
import time
import os
import requests, json
import random
import pyttsx3


engine = pyttsx3.init()


startup = ["Starting Ares...", "Booting up Ares..."]
selected_startup = random.choice(startup)


print(selected_startup)
engine.say(selected_startup)
engine.runAndWait()

command = input("Enter Command:")



def main():
    commands()

def commands():
    if command == "Hey" or "Hello" or "Hi" or "Ares":
        greetings = ["Hello, sir. What can I help you with?", "How can I be of assistance today, sir?", "Yes?"]
        selected_greeting = random.choice(greetings)
        print(selected_greeting)
        engine.say(selected_greeting)
        engine.runAndWait()


    elif command == "how are you":
        print("I am well, thank you. What can I do for you?")

    elif command == "What's the time?" or "What time is it?" or "What's the time, Ares?" or "What time is it, Ares" or "Time":
        dates = ["Todays date is ", "Today is "]
        selected_date = random.choice(dates)
        print(ctime())
        engine.say(selected_date + ctime)
        engine.runAndWait


    elif command == "Stop":
        print("Shutting down...")
    

    else:
        print("I'm sorry, I didn't quite catch that.")        

if __name__ == '__main__':
    commands()

因此,例如,如果我说“嘿”,它应该以其中一种问候作为回应,但无论我输入什么,它总是以问候回应。

我不知道它有什么问题。这可能真的很简单。感谢你们 :)

标签: pythonif-statement

解决方案


哦!谢谢你。问题是我用

command == "something"

而不是使用

command in {"something"}

再次感谢你


推荐阅读