UnboundLocalError:无法访问未与值关联的局部变量“print”?

问题描述 投票:0回答:1

这是代码

def takeCommand() :
    r = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as source :
        print("Listening......")
        r.pause_threshold = 1
        r.energy_threshold = 300
        audio = r.listen(source,0,4)

    try :
        print("Understanding.....")
        query = r.recognize_google(audio,language='en-in')
        print = (f"You Said: {query}\n")
    except Exception as e:
        print("Say That Again")
        return "None" 
    return query

这是终端响应

Traceback (most recent call last):
  File "c:\Users\bhave\OneDrive\Desktop\JARVIS\YouTube\Jarvis_main.py", line 33, in <module>
    query = takeCommand().lower() 
            ^^^^^^^^^^^^^
  File "c:\Users\bhave\OneDrive\Desktop\JARVIS\YouTube\Jarvis_main.py", line 17, in takeCommand
    print("Listening......")
    ^^^^^
UnboundLocalError: cannot access local variable 'print' where it is not associated with a value

这是我删除这个之后的

engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty("voices",voices[0].id)
engine.setProperty("rate",170)

engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty("voices",voices[0].id)
engine.setProperty("rate",170)

打印问题可能是此问题的根源 请尽快解决这个问题

python python-3.x debugging speech-recognition text-to-speech
1个回答
0
投票
print = (f"You Said: {query}\n")

需要

print(f"You Said: {query}\n")

您通过声明一个名为

print
的变量来覆盖
print
函数。然后,在稍后的执行过程中,您尝试正常使用
print
,但它试图访问仅在函数的本地范围内定义的变量。

© www.soinside.com 2019 - 2024. All rights reserved.