我不知道如何在提示原始输入后,在while循环的第二次迭代中避免eoferor。我需要这个循环运行30次,理想情况下不会有一个try-except块从程序启动我。任何帮助都将不胜感激!`在import sys
import pyaudio
import wave
import os
import fcntl
import time
def main():
sound_cues = open('sound_cues').read().splitlines()
light_cues = open('light_cues').read().splitlines()
play_names = open('play_names').read().splitlines()
plays = []
for idx in range(len(play_names)):
plays.append( {'name': play_names[idx], 'sound': sound_cues[idx], 'lights': light_cues[idx]} )
while(True):
play_num = raw_input("Enter a play number: ")
play = plays[int(play_num) - 1]
print 'lights:'
print play['lights']
raw_input("Press Enter to start sound...")
print 'sound:'
print play['sound']
audio = "sounds/" + play['sound']
wf = wave.open(audio)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output=True)
data = wf.readframes(1024)
fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK)
print "Press Enter to stop audio..."
while data != '':
try:
stdin = sys.stdin.read()
if "\n" in stdin or "\r" in stdin:
break
except IOError:
pass
stream.write(data)
data = wf.readframes(1024)
if __name__ == "__main__":
main()
`
输出是`
^{pr2}$
`