如何使用触发器停止无限循环?
How can I stop the infinite loop with a trigger?
我想让程序播放声音直到用户自己停止程序。
我的代码:
import pathlib,time
from pydub import AudioSegment
from pydub.playback import play
from gtts import gTTS
english=["school", "book", "theater"]
select = str(input("press ENTER to Start Stop=>'s':"))
elements=('', 'p')
while True:
if select not in elements:
print("Please Correct Selection--")
continue
elif select in elements:
for pick in english:
index = english.index(pick)
eng = pick
print(index, "-> ", eng)
text_eng = eng
my_eng_obj = gTTS(text=eng, lang="en", slow=False)
text_eng = text_eng.replace(" ", "_")
try:
file_en = pathlib.Path(text_eng + ".mp3")
if file_en.exists():
song_s = AudioSegment.from_mp3(file_en)
play(song_s)
pass
else:
my_eng_obj.save(text_eng + ".mp3")
song_s = AudioSegment.from_mp3(file_en)
play(song_s)
except:
pass
time.sleep(0.2)
声音部分工作正常。我需要无限循环部分。
如何用输入中断?
试试这个:
trigger = False
while True: # infinite loop
# Do something
# Do something to make trigger True. Keep it false if you want to keep the loop running
if 1 + 1 == 2: # Example comparison
trigger = True
print("Hello World")
if trigger:
break
你可以试试这个:
# Modify user message
select = str(input("press ENTER to Start Stop=>CTRL+C:"))
# Use a try/except within the while loop
while True:
try:
if select not in elements:
# ...
elif select in elements:
# ...
except KeyboardInterrupt:
break
这是解决方案之一
import time
from pynput import keyboard
print("Press Enter ==> Start\n"
"Press p ==> Exit")
check = ''
def Start(key):
if key == keyboard.Key.enter:
print("Started")
return False
def Stop(key):
global check
if key==keyboard.KeyCode(char='p'):
check = 'stop'
return False
with keyboard.Listener(on_release=Start) as listener:
listener.join()
listener = keyboard.Listener(on_release=Stop)
listener.start()
while True:
print("Do Something:Test")
time.sleep(1)
if check == 'stop':
print("\nLoop Stopped...!")
break
我想让程序播放声音直到用户自己停止程序。
我的代码:
import pathlib,time
from pydub import AudioSegment
from pydub.playback import play
from gtts import gTTS
english=["school", "book", "theater"]
select = str(input("press ENTER to Start Stop=>'s':"))
elements=('', 'p')
while True:
if select not in elements:
print("Please Correct Selection--")
continue
elif select in elements:
for pick in english:
index = english.index(pick)
eng = pick
print(index, "-> ", eng)
text_eng = eng
my_eng_obj = gTTS(text=eng, lang="en", slow=False)
text_eng = text_eng.replace(" ", "_")
try:
file_en = pathlib.Path(text_eng + ".mp3")
if file_en.exists():
song_s = AudioSegment.from_mp3(file_en)
play(song_s)
pass
else:
my_eng_obj.save(text_eng + ".mp3")
song_s = AudioSegment.from_mp3(file_en)
play(song_s)
except:
pass
time.sleep(0.2)
声音部分工作正常。我需要无限循环部分。 如何用输入中断?
试试这个:
trigger = False
while True: # infinite loop
# Do something
# Do something to make trigger True. Keep it false if you want to keep the loop running
if 1 + 1 == 2: # Example comparison
trigger = True
print("Hello World")
if trigger:
break
你可以试试这个:
# Modify user message
select = str(input("press ENTER to Start Stop=>CTRL+C:"))
# Use a try/except within the while loop
while True:
try:
if select not in elements:
# ...
elif select in elements:
# ...
except KeyboardInterrupt:
break
这是解决方案之一
import time
from pynput import keyboard
print("Press Enter ==> Start\n"
"Press p ==> Exit")
check = ''
def Start(key):
if key == keyboard.Key.enter:
print("Started")
return False
def Stop(key):
global check
if key==keyboard.KeyCode(char='p'):
check = 'stop'
return False
with keyboard.Listener(on_release=Start) as listener:
listener.join()
listener = keyboard.Listener(on_release=Stop)
listener.start()
while True:
print("Do Something:Test")
time.sleep(1)
if check == 'stop':
print("\nLoop Stopped...!")
break