为了账号安全,请及时绑定邮箱和手机立即绑定

从用户输入在 python 中循环

从用户输入在 python 中循环

大话西游666 2022-08-02 18:15:37
我试图让一个python文本到语音程序无限运行,直到用户决定退出,任何帮助将不胜感激这是我的代码:# IMPORTimport gtts as gTTSimport os # TTSmyText = input("Enter your text: ")language = 'en'output = gTTS.gTTS(text=myText,, slow=False,)output.save("output.mp3")os.system("start output.mp3")# Restartdef 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 answerprint(get_answer("yes or no? "))
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

# 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,, 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


查看完整回答
反对 回复 2022-08-02
  • 1 回答
  • 0 关注
  • 106 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信