Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 16-Sep-21 2:26am

 
Share this answer
 
Comments
Member 15355985 13-Sep-21 5:36am    
OriginalGriff
thank you for your answer, yes i think this is what iam looking for,can you please show me how to class it in my code.
thank you so much
OriginalGriff 13-Sep-21 5:57am    
Follow the link, and read it. It has examples.
Member 15355985 15-Sep-21 2:40am    
hi there,thank you for your help,i know iam asking a lot,iam just trying to learn,do you know someone who will help me to fix the problem i will pay him of course if he dont ask for a lot,thank you
Just add a else statement

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")
 
Share this answer
 
Comments
Member 15355985 16-Sep-21 14:31pm    
lol anyway thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900