import pyttsx3 #pip install pyttsx3 import speech_recognition as sr #pip install speechRecognition import datetime import webbrowser import os import smtplib engine = pyttsx3.init('sapi5') voices = engine.getProperty('voices') # print(voices[1].id) engine.setProperty('voice', voices[0].id) def speak(audio): engine.say(audio) engine.runAndWait() def wishMe(): hour = int(datetime.datetime.now().hour) if hour>=0 and hour<12: speak("hello!") elif hour>=12 and hour<18: speak("Good Afternoon!") else: speak("i hope you are ok") def takeCommand(): #It takes microphone input from the user and returns string output r = sr.Recognizer() with sr.Microphone() as source: print("Listening...") r.pause_threshold = 1 audio = r.listen(source) try: query = r.recognize_google(audio, language='en-in') print("Recognizing...") print(f"User said: {query}\n") except Exception as e: print(e) print("Unable to Recognize your voice.") return query if __name__ == "__main__": wishMe() while True: # if 1: query = takeCommand().lower() # Logic for executing tasks based on query if 'how are you' in query: speak("im ok thank you") if 'how was your day' in query: speak('my day was ok') elif 'what did you eat' in query: speak('i ate ramen noodles') ----------------------------------------- my question is how to create an error or an Exception for (not in query) for example if a question is not in query speak (sorry i dont know that) ---------------------------------------- thank you guys i hope you will understand my question and what iam looking for i will appreciate any help . than you. What I have tried: python iam new in python i didnt try anything since i dont have any idea. thank you
if 'how are you' in query: speak("im ok thank you") if 'how was your day' in query: speak('my day was ok') elif 'what did you eat' in query: speak('i ate ramen noodles') else: speak("Unable to Resolve to query")
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)