Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python being weird
#1
I am trying to run a script within the python idle and every time it does, it does not run properly, and does this weird thing of popping up "Windows Installer" and only goes away when killing the script. The script is a virtual assistant and runs with no errors but does not track my mic even though i made sure its plugged in and working.

IMAGE:
[Image: n8DRqsP]

CODE:
import os
import sys
import datetime
import pyttsx3
import speech_recognition as sr
import wikipedia
import wolframalpha
import webbrowser
import smtplib
import random

engine = pyttsx3.init('sapi5')

client = wolframalpha.Client('Get your own key')

voices = engine.getProperty('voices')
engine.setProperty('voice', voices[len(voices) - 2].id)


def talk(audio):
    print('KryptoKnite: ' + audio)
    engine.say(audio)
    engine.runAndWait()


def greetMe():
    CurrentHour = int(datetime.datetime.now().hour)
    if CurrentHour >= 0 and CurrentHour < 12:
        talk('Good Morning!')

    elif CurrentHour >= 12 and CurrentHour < 18:
        talk('Good Afternoon!')

    elif CurrentHour >= 18 and CurrentHour != 0:
        talk('Good Evening!')


greetMe()

talk('Hey Buddy, It\'s  your assistant KryptoKnite!')
talk('tell me about today?')


def GivenCommand():
    k = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        k.pause_threshold = 1
        audio = k.listen(source)
    try:
        Input = k.recognize_google(audio, language='en-in')
        print('Kunal Dhariwal: ' + Input + '\n')

    except sr.UnknownValueError:
        talk('Sorry! I didn\'t get that! Try typing it here!')
        Input = str(input('Command: '))

    return Input


if __name__ == '__main__':

    while True:

        Input = GivenCommand()
        Input = Input.lower()

        if 'open google' in Input:
            talk('sure')
            webbrowser.open('www.google.co.in')

        elif 'open gmail' in Input:
            talk('sure')
            webbrowser.open('www.gmail.com')
            
        elif 'open youtube' in Input:
            talk('sure')
            webbrowser.open('www.youtube.com')

        elif "what\'s up" in Input or 'how are you' in Input:
            setReplies = ['Just doing some stuff!', 'I am good!', 'Nice!', 'I am amazing and full of power']
            talk(random.choice(setReplies))
       
        elif "who are you" in Input or 'where are you' in Input or 'what are you' in Input:
            setReplies = [' I am KryptoKnite', 'In your system', 'I am an example of AI']
            talk(random.choice(setReplies))

        elif 'email' in Input:
            talk('Who is the recipient? ')
            recipient = GivenCommand()

            if 'me' in recipient:
                try:
                    talk('What should I say? ')
                    content = GivenCommand()

                    server = smtplib.SMTP('smtp.gmail.com', 587)
                    server.ehlo()
                    server.starttls()
                    server.login("Your_Username", 'Your_Password')
                    server.sendmail('Your_Username', "Recipient_Username", content)
                    server.close()
                    talk('Email sent!')

                except:
                    talk('Sorry ! I am unable to send your message at this moment!')

        elif 'nothing' in Input or 'abort' in Input or 'stop' in Input:
            talk('okay')
            talk('Bye, have a good day.')
            sys.exit()

        elif 'hello' in Input:
            talk('hey')

        elif 'bye' in Input:
            talk('Bye, have a great day.')
            sys.exit()


        elif 'play music' in Input:
            music_folder = 'C:\\Users\\Public\\Music\\'
            music = ['friends']
            random_music = music_folder + random.choice(music) + '.mp3'
            os.system(random_music)

            talk('Okay, here is your music! Enjoy!')

        elif 'show images' in Input:
            images_folder = 'C:\\Users\\Public\\Pictures\\'
            images = ['kunal']
            random_images = images_folder + random.choice(images) + '.jpeg'
            os.system(random_images)

            talk('Okay, here are your images! Have Fun!')


        else:
            Input = Input
            talk('Searching...')
            try:
                try:
                    res = client.Input(Input)
                    outputs = next(res.outputs).text
                    talk('Alpha says')
                    talk('Gotcha')
                    talk(outputs)

                except:
                    outputs = wikipedia.summary(Input, sentences=3)
                    talk('Gotcha')
                    talk('Wikipedia says')
                    talk(outputs)


            except:
                    talk("searching on google for " + Input)
                    say = Input.replace(' ', '+')
                    webbrowser.open('https://www.google.co.in/search?q=' + Input)



        talk('Next Command! Please!')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,422 Jan-18-2023, 04:51 PM
Last Post: mikepy
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,604 May-27-2021, 02:01 AM
Last Post: MDRI
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,094 Jul-22-2019, 06:14 AM
Last Post: buran
  Python 3.6.5 pathlib weird behaviour when resolve a relative path on root (macOs) QbLearningPython 7 6,097 May-29-2018, 08:38 AM
Last Post: QbLearningPython
  python nested list assignment weird behavior eyalk1 2 4,392 Jan-16-2018, 07:32 PM
Last Post: wavic
  Python beginner: Weird Syntax Error mentoly 5 10,248 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020