vendredi 19 juin 2020

I'm having issues running this code cause it keeps executing random portions of the if statements in Python

So I'm working on an assistant type program but I'm planning on customizing it a little more for a more particular usage than your usual assistant, I'm having an issue with my code where sometimes it would execute a random if statement along side the if statement that was supposed to be executed and sometimes it would just throw an error and I have no idea why this happens, at least not yet.. Any help would be appreciated.

EDIT: I updated the code to contain elif (CODE Update #2)

def check_audio():
cmdtext = get_audio()
print("CMDTEXT-You said: " + cmdtext)

try:
    if any(x.lower() in cmdtext for x in ["Hey", "Hello", "Ahoi"]):
        hellos = ["Hey there!", "Hi!", "How's it going?", "Well.. Hello there!"]
        stalk(random.choice(hellos))
    elif "schedule" in cmdtext:
        if "days" or "day" in cmdtext:
            number = re.search(r'\d+', cmdtext).group()
            if w2n.word_to_num(cmdtext) == int:
                get_events(w2n.word_to_num(cmdtext), service)
            elif "Week" in cmdtext:
                get_events(7, service)
                if w2n.word_to_num(cmdtext) & "Week" in cmdtext:
                    combined = w2n.word_to_num(cmdtext) * 7
                    get_events(number, service)

    elif "quit" in cmdtext:
        quitcount + 1
        print(quitcount)
        byebyes = ["Bye Bye!", "See you later!", "Cya later alligator", "See you soon!"]
        stalk(random.choice(byebyes))
        quit()
    elif "go back" in cmdtext:
        #This part is not currently functional and does absolutely nothing
        while "go back" in cmdtext:
            break
    elif "open" in cmdtext:
        if "Notepad" in cmdtext:
            subprocess.Popen([r'C:\Program Files\Notepad++\\Notepad++.exe'])
        elif "Chrome" in cmdtext:
            subprocess.Popen([r'C:\Program Files (x86)\Google\Chrome\Application\\Chrome.exe'])
        elif "Discord" in cmdtext:
            subprocess.Popen([r'C:\Users\CelestialMavis\AppData\Local\Discord\app-0.0.306\\Discord.exe'])
        elif "Steam" in cmdtext:
            subprocess.Popen([r'C:\Program Files (x86)\Steam\\Steam.exe'])
        else:
            stalk("I did not recognize that program name")
    if any(y.lower() in cmdtext for y in ["Rick Roll", "rickroll"]):
        webbrowser.open_new_tab("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
    else:
        print("I don't recognise this command")

except Exception as e:
    print("Error" + e)
    stalk("I didn't quite get that")

EDIT#1 is there a way to make the program go back to the previous .py file where the code first ran and start at a certain point, I run a listener in the first file that listens for commands and once it gets a command it runs this .py file that identifies the command and the issue is once its done it continues to listen for commands instead of going back to listen for the keyword here is the code i'm using:

def mainOps():

   while beginop != "Quit" or "Exit":
       SEGI.check_audio()

# Listen for Keyword from Microphone!
recog = spcrec.Recognizer()

# This is the keyword that will be used to activate the system in this case its "hey Wendy"
cmdwrd = 'hey Wendy'
# This is the keyword that will cause the program to quit
cmdwrdqt = 'quit'

# This while loop listens for the keyword and then executes mainOps() if it's detected

with spcrec.Microphone() as source:
#with spcrec.AudioFile('keyword.wav') as source:

    print(f"Hey! Im VASE")
    print(f"Please say {cmdwrd.capitalize()} to speak with Wendy or say {cmdwrdqt.capitalize()} to End the program")
    # The variable below dictates what VASE TTS says upon startup
    Greeting = f"Hey! I am VASE"
    whattosay = f"Please say {cmdwrd} to speak with Wendy or say {cmdwrdqt} to End the program"

    # Sending the variable to the TTS function in order to create an MP3 file of the text and play it
    vtts.VaseTTS(whattosay) #Strangely this only works once and I have no idea why.
    # Attempted to call the function again using different text but it doesn't function yet
    vso = "Vase Online"
    print(vso)
    #STa.talk(vso)

    # Code below continues to listen to what you say and does something when it hears the keyword
    while True:
        try:
            STa.detectionwords = STa.get_audio()

            if (STa.check_audio() == True):
                while True:
                 mainOps()
            else:
                print("Please speak again, keyword not detected")
        except Exception as e:
            print('No keywords detected, please say the command to begin again')
            print(e)

Aucun commentaire:

Enregistrer un commentaire