Jan-10-2023, 05:16 PM
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:
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()