简体   繁体   中英

Loop in python from user input

I trying to get a python text to speech program running infinitely until user decides to quit, any help would be greatly appreciated

Here's my code:

# 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? "))
# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM