-2

I'm using pyttsx3 to make a talking bot . and it work

import pyttsx3
f = pyttsx3.init()

a = input("type something: ")

if a == "hello":
    print(f.say("Hi How are you"))

f.runAndWait()

but if I want the code to repeat the operation when ever its end I mean if I typed something and got the respond I will get another "type something" input without duplicate the code like this

import pyttsx3
f = pyttsx3.init()

a = input("type something: ")

if a == "hello":
    print(f.say("Hi How are you"))
f.runAndWait()

b = input("type something: ")

if b == "hi":
    print(f.say("Hi"))
f.runAndWait()
c = input("type something: ")

if c == "hey":
    print(f.say("hi hows going"))

f.runAndWait()

i tried making a loop but didn't work

import pyttsx3
f = pyttsx3.init()

a = input("type something: ")

if a == "hello":
    print(f.say("Hi How are you"))

for a in f:
    print(a)

f.runAndWait()

So its going to ask me the a input again and again with no need to duplicate the code

JackQx
  • 182
  • 1
  • 13
  • Put it in a function. – khelwood Aug 29 '21 at 22:31
  • @khelwood i really don't know How? – JackQx Aug 29 '21 at 22:33
  • @JackQx [this](https://www.amazon.com/Learning-Python-5th-Mark-Lutz/dp/1449355730/ref=sr_1_1?dchild=1&keywords=learning+python&qid=1630276449&sr=8-1) book should help – Ahmed I. Elsayed Aug 29 '21 at 22:34
  • i added my work last part of the question – JackQx Aug 29 '21 at 22:34
  • 1
    If you need this kind of help, then you need to follow a tutorial and study the fundamentals of the language, and not even *think* about trying to use third-party libraries like `pyttsx3` until you've worked through that. This is like if you wanted to become a grandmaster at chess but couldn't be bothered to learn the rules first, just jumping right into strategy. – Karl Knechtel Aug 29 '21 at 22:35
  • https://docs.python.org/3/tutorial/controlflow.html#defining-functions – khelwood Aug 29 '21 at 22:35
  • @KarlKnechtel I know how the function works But i dont know how to use it in this case I mean just I cant imagine how I can make it in a function so it will go in loop – JackQx Aug 29 '21 at 22:38
  • 1
    Then my answer remains the same. You don't actually know how functions work if you are not able to write functions that are useful to solve elementary problems. You don't actually know how loops work if you are not able to reason about what code should go inside the loop, or about how to repeat code indefinitely (rather than trying to iterate over a sequence that isn't there). You don't actually know how any of this works, yet, if you find anything in this comment confusing or need to look up any terms. There is no avoiding the fundamentals. – Karl Knechtel Aug 29 '21 at 22:41
  • @JackQx for applications that run indefinitely, a common pattern is an _infinite while loop_ sometimes called an _event loop_. You check for input, do different things based what it was, then cycle around again. Just remember to put in a way to break out of it. Start by looking at this: https://realpython.com/python-while-loop/#infinite-loops Keep learning and playing! – Davos Aug 31 '21 at 14:27

1 Answers1

1

I think it should work, I really didn't understand your question, but this is closest to what you asked for

import pyttsx3
f = pyttsx3.init()
while True:
    a = input("type something: ")
    if a == "hello":
        print(f.say("Hi how are you?"))
    elif a == "hi":
        print(f.say("Hi"))
    elif a == "hey":
        print(f.say("hi hows going"))
    f.runAndWait()

or if you want to try functions you can try:

import pyttsx3
f = pyttsx3.init()
while True:
    nameoffunction()

def nameoffunction():
    a = input("type something: ")
    if a == "hello":
        print(f.say("Hi how are you?"))
    elif a == "hi":
        print(f.say("Hi"))
    elif a == "hey":
        print(f.say("hi hows going"))
    f.runAndWait()
heyn
  • 56
  • 4
  • this is exactly what I need . to ask me again and again without needed to duplicate some part of the code – JackQx Aug 29 '21 at 22:42