I’m using pyttsx3 to make a talking bot . and it work
JavaScript
x
10
10
1
import pyttsx3
2
f = pyttsx3.init()
3
4
a = input("type something: ")
5
6
if a == "hello":
7
print(f.say("Hi How are you"))
8
9
f.runAndWait()
10
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
JavaScript
1
21
21
1
import pyttsx3
2
f = pyttsx3.init()
3
4
a = input("type something: ")
5
6
if a == "hello":
7
print(f.say("Hi How are you"))
8
f.runAndWait()
9
10
b = input("type something: ")
11
12
if b == "hi":
13
print(f.say("Hi"))
14
f.runAndWait()
15
c = input("type something: ")
16
17
if c == "hey":
18
print(f.say("hi hows going"))
19
20
f.runAndWait()
21
i tried making a loop but didn’t work
JavaScript
1
13
13
1
import pyttsx3
2
f = pyttsx3.init()
3
4
a = input("type something: ")
5
6
if a == "hello":
7
print(f.say("Hi How are you"))
8
9
for a in f:
10
print(a)
11
12
f.runAndWait()
13
So its going to ask me the a input again and again with no need to duplicate the code
Advertisement
Answer
I think it should work, I really didn’t understand your question, but this is closest to what you asked for
JavaScript
1
12
12
1
import pyttsx3
2
f = pyttsx3.init()
3
while True:
4
a = input("type something: ")
5
if a == "hello":
6
print(f.say("Hi how are you?"))
7
elif a == "hi":
8
print(f.say("Hi"))
9
elif a == "hey":
10
print(f.say("hi hows going"))
11
f.runAndWait()
12
or if you want to try functions you can try:
JavaScript
1
15
15
1
import pyttsx3
2
f = pyttsx3.init()
3
while True:
4
nameoffunction()
5
6
def nameoffunction():
7
a = input("type something: ")
8
if a == "hello":
9
print(f.say("Hi how are you?"))
10
elif a == "hi":
11
print(f.say("Hi"))
12
elif a == "hey":
13
print(f.say("hi hows going"))
14
f.runAndWait()
15