2017-08-15 58 views
1

我正在开发一个项目,让计算机在我给它命令时回声。我刚开始,但是当我运行代码时,它没有以我期望的方式做出响应。这里是我的代码:检查里面的循环未执行

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 

感谢, 阿迪亚

+1

这可能太简单了,但你确定你的'if'语句正在执行'for'循环吗?它看起来像你的'for'循环只是打印每个短语而不检查短语的值是否是'哦'。在'print'之前尝试添加一个换行符并缩进该行,以及它下面的两行嵌套在'for'循环中。 – cole

+0

@cole谢谢,但它不起作用 – Aditya

+0

你好?任何人? – Aditya

回答

1

这是因为if语句不在循环,循环的身体是print语句

for phrase in LiveSpeech(): 
    print(phrase) 
    if phrase == 'oh' : 
     os.system('espeak' ' "hi"') 

这是它应该看起来像

+0

在我的电脑上,它不起作用。 – Aditya

+0

你必须使用标签。这就是Python用来解决范围 – MatTheWhale

+0

@MatTheWhale我不是在说这个,上面的答案在我的电脑上不起作用。 – Aditya