Jan-10-2023, 05:16 PM
(This post was last modified: Jan-10-2023, 05:22 PM by snippsat.
Edit Reason: Added code tag
)
I kept having this error: UnboundLocalError: local variable 'command' referenced before assignment
Does Anybody know how to solve it?
Here is the code:
Does Anybody know how to solve it?
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
import speech_recognition as sr import pyttsx3 import pywhatkit import datetime import wikipedia import pyjokes import random as rd thx1 = [ "Your Welcome" , "No problem" , "Its my job to help you" ] listener = sr.Recognizer() engine = pyttsx3.init() voices = engine.getProperty( 'voices' ) engine.setProperty( 'voice' , voices[ 1 ]. id ) def talk(text): engine.say(text) engine.runAndWait() print ( "Im Ava what can i do for you?" ) talk( "Im Ava... what can i do for you?" ) print ( "If you need any help say help" ) talk( "If you need any help say help" ) def take_command(): try : with sr.Microphone() as source: print ( "Listening..." ) voice = listener.listen(source) command = listener.recognize_google(voice) command = command.lower() print (command) if 'ava' in command: command = command.replace( 'ava' , '') except : pass return command def run_ava(): command = take_command() if 'play' in command: song = command.replace( 'play' , '') talk( 'playing' + song) pywhatkit.playonyt(song) elif 'help' in command: print ( "Here is what i can: play music on youtube, search something on wiki, tell jokes, tell time now" ) talk( 'Here is what i can' ) elif 'hear me' in command: print ( "Yes, I can hear you clear" ) talk( "Yes, I can hear you clear" ) elif 'who are you' in command: print ( 'Im Ava your virtual assistent' ) talk( 'Im Ava... your virtual assistent' ) elif 'how are you' in command: print ( "Im glad you asked. Im good thank you" ) talk( "Im glad you asked. Im good thank you" ) elif 'thanks' in command: thx = rd.choice(thx1) print (thx) talk(thx) elif 'thank you' in command: thx = rd.choice(thx1) print (thx) talk(thx) elif 'male or female' in command: print ( 'Male, Female, Nonbinary? Nah bro im a walmart bag' ) talk( 'male... female... nonbinary? nah broo.. im a walmart bag' ) elif 'time' in command: time = datetime.datetime.now().strftime( '%H:%M' ) print (time) talk( 'Its' + time) elif 'tell me something about' in command: person = command.replace( 'tell me something about' , '') info = wikipedia.summary(person, 5 ) print (info) talk(info) elif 'who is' in command: person = command.replace( 'who is' , '') info = wikipedia.summary(person, 5 ) print (info) talk(info) elif 'what is' in command: person = command.replace( 'what is' , '') info = wikipedia.summary(person, 5 ) print (info) talk(info) elif 'joke' in command: joke = pyjokes.get_joke() print (joke) talk(joke) else : talk( 'Im not sure i understand' ) while True : run_ava() |