jeudi 3 décembre 2020

What is wrong with my "If Statement" in line 44 and my addition to variable statement in line 37?

I am currently making the rough outline for a voice controller so I can stop designing parts, but even while making the rough outline I ran into some issues with my rusty skill level in if statement and syntax of variable addition. I have done a lot of google research as well as checked out similar problems on this page. If anything is wrong please let me know instead of just downvoting. The specific error is that the program exits out after the first time I ask the script how its doing rather than continuing to run like it should if I remove the line with the variable addition along with the chunk about the differed statement that replaces the old "how are you" after it being asked once.

from time import ctime
import time
import os
import pyttsx3
import random


repetitionsocial1=0

numberList = ["Thanks for asking. But I am a computer","222","I eat poop","444","555"]

def recordAudio():
    # Record Audio
    r = sr.Recognizer()
    with sr.Microphone() as source:
     print("Say something!")
     audio = r.listen(source)

    # Speech recognition using Google Speech Recognition
    data = ""
    try:
        # Uses the default API key
        # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
        data = r.recognize_google(audio)
        print("You said: " + data)
    except sr.UnknownValueError:
        print("Offline Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Offline Recognition service; {0}".format(e))

    return data

def jarvis(data):
    global repetitionsocial1
    if "how are you" in data and repetitionsocial1==0 :
        repetitionsocial1=repetitionsocial1+1
        engine = pyttsx3.init()
        engine.say("What answer do you expect. I am a Computer?")
        engine.runAndWait()
        
        

    if "how are you" in data and repetitionsocial1>=0:
        engine = pyttsx3.init()
        engine.say("I am still fine, but again, I am a computer. You have asked me this"+str(repetitionsocial1)+"times")
        engine.runAndWait()

    if "what time is it" in data:
        engine = pyttsx3.init()
        engine.say(ctime())
        engine.runAndWait()

   # if "where is" in data:
    #    data = data.split(" ")
     #   location = data[2]
      #  speak("Hold on Frank, I will show you where " + location + " is.")
       # os.system("chromium-browser https://www.google.nl/maps/place/" + location + "/&")

# initialization
time.sleep(2)
engine = pyttsx3.init()
engine.say("Hi Frank, what can I do for you?")
engine.runAndWait()

while 1:
    data = recordAudio()
    jarvis(data)

Aucun commentaire:

Enregistrer un commentaire