从用户输入在python中循环

2024-04-19 21:00:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让python文本到语音程序无限期地运行,直到用户决定退出,任何帮助都将不胜感激

这是我的密码:

# IMPORT
import gtts as gTTS
import os 

# TTS

myText = input("Enter your text: ")

language = 'en'

output = gTTS.gTTS(text=myText, lang=language, slow=False,)


output.save("output.mp3")

os.system("start output.mp3")

# Restart

def get_answer(prompt):
    answer = input(prompt)
    # 1 while not (answer == "yes" or answer == "no"
    # 2 while answer not in ("yes", "no"):
    # 3 while answer not in ["yes", "no"]:
    while answer not in ("yes", "no"):
        answer = input(prompt)
    return answer

print(get_answer("yes or no? "))

Tags: notextanswerinimportinputoutputos
1条回答
网友
1楼 · 发布于 2024-04-19 21:00:17
# IMPORT
import gtts as gTTS
import os 

# FUNCTIONS
def get_answer(prompt):
    answer = input(prompt)
    # 1 while not (answer == "yes" or answer == "no"
    # 2 while answer not in ("yes", "no"):
    # 3 while answer not in ["yes", "no"]:
    while answer not in ("yes", "no"):
        answer = input(prompt)
    return answer

# TTS
while True:
    myText = input("Enter your text: ")

    language = 'en'

    output = gTTS.gTTS(text=myText, lang=language, slow=False,)


    output.save("output.mp3")

    os.system("start output.mp3")

    # Restart
    if get_answer("quit (yes or no?): ") == yes: break #break out of the loop

相关问题 更多 >