问题描述
#code 正在运行,但无法访问的部分不起作用,请帮助我
os.chdir(r'C:\Users\berka\OneDrive\Masaüstü\Masaüstü\melon')
while True:
files = os.listdir("./")
for file in files:
if os.path.isfile(file) and file !="melon.py" and file != "fileOrganiser.bat" and file != "fileOrganiser.vbs":
ext = (file.split(".")[-1]).lower()
if ext in image_formats:
shutil.move(file,"images/"+file)
elif ext in audio_formats:
shutil.move(file,"audio/"+file)
elif ext in video_formats:
shutil.move(file,"videos/"+file)
elif ext in docs_formats:
shutil.move(file,"docs/"+file)
else:
shutil.move(file,"others/"+file)
#ıt 在此之后无法工作
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices[1].id)
engine.setProperty('voice',voices[1].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
#你知道如何修复吗
#我试着做一个语音助手,结果成功了。然后我尝试在Windows打开时运行它,我发现了这样的代码,但我遇到了这个问题。
解决方法
当你有一个 while 循环时,循环内的代码会一次又一次地执行,直到你声明的语句为假。因此,当您的语句为 while True:
时,true 永远不会简单地为 false,这就是 while
循环中的代码一次又一次无限循环的原因。您需要设置一些变量,比如 run_program = True
而是 while True:
写 while run_program == True:
,并且当您需要停止该 while
循环以在 { 之后继续执行您的代码时{1}} 循环,只需调用 while
。这就是为什么您会收到错误消息,指出 run_program = False
循环后的代码无法访问。