我正在研究一个项目,当我给它命令时让计算机回复。我刚刚开始,但是当我运行代码时,它并没有按照我期望的方式做出响应。这是我的代码:
from pocketsphinx import LiveSpeech
import os
for phrase in LiveSpeech(): print(phrase)
if phrase == 'oh' :
os.system('espeak' ' "hi"')
运行此代码时出错。这是错误:
Traceback (most recent call last):
File "xxt", line 4, in <module>
print(phrase)
File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 93, in __str__
return self.hypothesis()
File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 126, in hypothesis
hyp = self.hyp()
File "/Library/Python/2.7/site-packages/pocketsphinx/pocketsphinx.py", line 359, in hyp
return _pocketsphinx.Decoder_hyp(self)
File "/Library/Python/2.7/site-packages/pocketsphinx/__init__.py", line 225, in stop
raise StopIteration
StopIteration
谢谢, Aditya
答案 0 :(得分:1)
这是因为if语句不在循环中,循环体是print语句
for phrase in LiveSpeech():
print(phrase)
if phrase == 'oh' :
os.system('espeak' ' "hi"')
它应该是什么样子