darkstorm
darkstorm

Reputation: 13

Elif statement Misbehaving and open desktop.ini again and again. Why?

This code is misbehaving and I don't know why. It executes, again and again, the same elif statement without any input and return the desktop.ini file, please tell me how can i fix this and why is this happening. If i put only 5 or 6 elif statements then its work fine but after 6th statement its not work or start misbehaving. desktop.ini file code is

[.ShellClassInfo]
LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21790
InfoTip=@%SystemRoot%\system32\shell32.dll,-12689
IconResource=%SystemRoot%\system32\imageres.dll,-108
IconFile=%SystemRoot%\system32\shell32.dll
IconIndex=-237


import pyttsx3
import wikipedia
import speech_recognition as sr
import os
import webbrowser
import datetime

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)


def speak(audio):
    engine.say(audio)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if 0 <= hour < 12:
        speak("Good Morning!")

    elif 12 <= hour < 18:
        speak("Good Afternoon!")

    else:
        speak("Good Evening!")

    speak("I am your assistant Sir. Please tell me how may I help you")

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)
        print("Say that again please...")
        return "None"
    return query


def wikipedia(query):
    speak('Searching Wikipedia...')
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences=2)
    speak("According to Wikipedia")
    print(results)
    speak(results)


def youtube():
    webbrowser.open("youtube.com")


def ongoogle(query):
    if "search" in query:
        query = query.replace("search", "")
    query = query.replace("on google", "")
    webbrowser.open(f"https://www.google.com/search?q={query}")


def google():
    webbrowser.open("google.com")


def yahoo():
    webbrowser.open("yahoo.com")


def music():
    music_dir = 'your path'
    songs = os.listdir(music_dir)
    print(songs)
    os.startfile(os.path.join(music_dir, songs[0]))


def musicyt():
    webbrowser.open(
        "https://www.youtube.com/watch?v=gvyUuxdRdR4&list=RDCLAK5uy_n9Fbdw7e6ap-98_A- 
8JYBmPv64v-Uaq1g&start_radio=1")


def thetime():
    strTime = datetime.datetime.now().strftime("%H:%M:%S")
    speak(f"Sir, the time is {strTime}")


def vscode():
    codePath = "your path"
    os.startfile(codePath)


def pycharm():
    codePath = "your path"
    os.startfile(codePath)


if __name__ == "__main__":
    wishMe()
    while True:
        # if 1:
        query = takeCommand().lower()
        if 'wikipedia' in query:
            wikipedia(query)

        elif 'open youtube' in query:
            youtube()

        elif 'on google' in query:
            ongoogle(query)

        elif 'open google' in query:
            google()

        elif 'open yahoo' in query:
            yahoo()

        elif 'play music' or 'play song' or 'play songs' in query:
            music()

        elif 'play music on youtube' or 'play song on youtube' or 'play songs on youtube' in query:
            musicyt()

        elif 'the time' in query:
            thetime()

        elif 'open vscode' in query:
            vscode()

        elif 'open pycharm' in query:
            pycharm()

Upvotes: -1

Views: 68

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 405785

The line

elif 'play music' or 'play song' or 'play songs' in query:

is probably not evaluated the way you think. The first two conditions just evaluate to the strings 'play music' and 'play song'. Only the third part of the condition is checking to see if a value is in query.

You need to check that each string is in the query variable.

elif 'play music' in query or 'play song' in query or 'play songs' in query:

Upvotes: 0

Related Questions