未定义文本



我是新编码的,正在做AI。我对打印(文本(有问题,我不知道如何解决这个问题。

def listen():
try:
with sr.Microphone() as source:
print(text)
voice = listener .listen(source)
rec = listener.recognize.google(voice, laguage='es-ES')
rec = rec.lower()
if name in rec:
rec = rec.replace(name, '')
print('Usted dijo: '+ rec)
except:
pass
return rec

text未定义,不能打印。在使用访问/使用变量之前,必须定义变量。

如果变量是在listen()函数之外定义的,则可以将参数传递给该函数。如果你不想传递一个参数,那么使用global(取决于其他情况,不要使用它(关键字来访问它

带有传递参数的示例:

def foo(text):
print(text)
text = "Bar"
foo(text)

输出:条形

带有global关键字的示例:

def foo():
global text
print(text)
text = "Bar"
foo()

输出:条形

最新更新