从用户输入在 python 中循环

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


这是我的代码:


# IMPORT

import gtts as gTTS

import os 


# TTS


myText = input("Enter your text: ")


language = 'en'


output = gTTS.gTTS(text=myText,, 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? "))


大话西游666
浏览 109回答 1
1回答

繁花不似锦

# IMPORTimport gtts as gTTSimport os # FUNCTIONSdef 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# TTSwhile True:    myText = input("Enter your text: ")    language = 'en'    output = gTTS.gTTS(text=myText,, 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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python