2017-08-15 92 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