ProgrammingHero1 / romantic-alexa

399 stars 524 forks source link

UnboundLocalError: local variable 'command' referenced before assignment #31

Closed asadkhalidd closed 3 years ago

asadkhalidd commented 3 years ago

Traceback (most recent call last): File "C:\Users\Asad\Desktop\romantic-alexa-main\romantic-alexa-main\main.py", line 60, in run_alexa() File "C:\Users\Asad\Desktop\romantic-alexa-main\romantic-alexa-main\main.py", line 35, in run_alexa command = take_command() File "C:\Users\Asad\Desktop\romantic-alexa-main\romantic-alexa-main\main.py", line 31, in take_command return command UnboundLocalError: local variable 'command' referenced before assignment

****ABOVE IS THE ERROR THAT I AM FACING AND BELOW IS THE CODE

import speech_recognition as sr import pyttsx3 import pywhatkit import datetime import wikipedia import pyjokes

listener = sr.Recognizer() engine = pyttsx3.init() voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id)

def talk(text): engine.say(text) engine.runAndWait()

def take_command(): try: with sr.Microphone() as source: print('listening...') voice = listener.listen(source) command = listener.recognize_google(voice) command = command.lower() if 'alexa' in command: command = command.replace('alexa', '') print(command) except: pass return command

def run_alexa(): command = take_command() print(command) if 'play' in command: song = command.replace('play', '') talk('playing ' + song) pywhatkit.playonyt(song) elif 'time' in command: time = datetime.datetime.now().strftime('%I:%M %p') talk('Current time is ' + time) elif 'who the heck is' in command: person = command.replace('who the heck is', '') info = wikipedia.summary(person, 1) print(info) talk(info) elif 'date' in command: talk('sorry, I have a headache') elif 'are you single' in command: talk('I am in a relationship with wifi') elif 'joke' in command: talk(pyjokes.get_joke()) else: talk('Please say the command again.')

while True: run_alexa()

FaikYY commented 3 years ago

I have the same problem, I'm using Arch Linux. Could you help us solving this one ?

asadkhalidd commented 3 years ago

I haven't been able to solve this problem yet l.

On Sat, Dec 26, 2020, 2:49 PM Faik YEŞİLYAPRAK notifications@github.com wrote:

I have the same problem, I'm using Arch Linux. Could you help us solving this one ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ProgrammingHero1/romantic-alexa/issues/31#issuecomment-751339716, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNMRLCYQNR64INVLKCIID3SWWWTRANCNFSM4VJGXC6A .

42sol-eu commented 3 years ago

Hi,

the error is solvable, due a error check or an initialization is missing

def take_command():
  command = '' # < add this line !
  try:
    with sr.Microphone() as source:
       print('listening...')
       voice = listener.listen(source)
       command = listener.recognize_google(voice)
  # ... more code

the underlying problem is bigger cause your with block or your listener seems to crash. The error on your system has to be in those lines I used in the code snippet.

Try to use a debugger and single step trough each line. If you post it here, we can try to solve it together.

asadkhalidd commented 3 years ago

Thanks, I'll check it out.

On Sun, Dec 27, 2020, 2:49 AM 42sol-eu notifications@github.com wrote:

Hi,

the error is solvable, due a error check or an initialization is missing

def take_command(): command = '' # < add this line ! try: with sr.Microphone() as source: print('listening...') voice = listener.listen(source) command = listener.recognize_google(voice)

... more code

the underlying problem is bigger cause your with block or your listener seems to crash. The error on your system has to be in those lines I used in the code snippet.

Try to use a debugger and single step trough each line. If you post it here, we can try to solve it together.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ProgrammingHero1/romantic-alexa/issues/31#issuecomment-751399472, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNMRLE7T3KP72LJNLFPX2LSWZK7BANCNFSM4VJGXC6A .

shahmalay95 commented 3 years ago

Screenshot_2020-12-29_04-10-02 Hi @42sol-eu , I have tried debugging as you said. Please help.

asadkhalidd commented 3 years ago

Ok, I will check.

On Tue, Dec 29, 2020, 2:14 PM shahmalay95 notifications@github.com wrote:

[image: Screenshot_2020-12-29_04-10-02] https://user-images.githubusercontent.com/20601968/103272925-11ee2180-498c-11eb-9760-cda765d3d9a4.png Hi @42sol-eu https://github.com/42sol-eu , I have tried debugging as you said. Please help.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ProgrammingHero1/romantic-alexa/issues/31#issuecomment-752006023, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMNMRLABRG7DBYA44NDKNJLSXGMXBANCNFSM4VJGXC6A .

Ggarg5551 commented 3 years ago

This is my code:- (Same as given)

import speech_recognition as sr import pyttsx3 import pywhatkit import datetime import wikipedia import pyjokes

listener = sr.Recognizer() engine = pyttsx3.init() voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id)

def talk(text): engine.say(text) engine.runAndWait()

def take_command(): command = '' try: with sr.Microphone() as source: print('listening...') voice = listener.listen(source) command = listener.recognize_google(voice) command = command.lower() if 'alexa' in command: command = command.replace('alexa', '') print(command) except: pass return command

def run_alexa(): command = take_command() print(command) if 'play' in command: song = command.replace('play', '') talk('playing ' + song) pywhatkit.playonyt(song) elif 'time' in command: time = datetime.datetime.now().strftime('%I:%M %p') talk('Current time is ' + time) elif 'who the heck is' in command: person = command.replace('who the heck is', '') info = wikipedia.summary(person, 1) print(info) talk(info) elif 'date' in command: talk('sorry, I have a headache') elif 'are you single' in command: talk('I am in a relationship with wifi') elif 'joke' in command: talk(pyjokes.get_joke()) else: talk('Please say the command again.')

while True: run_alexa()

But, when I run it, it shows no error, I had an error earlier, but after reading ' https://github.com/ProgrammingHero1/romantic-alexa/issues/31#issuecomment-751399472', I resolved the problem. Thank you for that...

But, now when I run it, Alexa (voice) says that "Please say the command again", which means it is unable to read to stop for us to take the command... What should I do?

Please tell me as soon as possible....

ashraf-minhaj commented 3 years ago

Please see issue #5