Python Forum
Help adding a loop inside a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help adding a loop inside a loop
#24
Got it, thanks.
    connection.execute("DELETE FROM reminders WHERE DateTimeStamp = ?", (Scheduled_Time,))
    connection.commit()
One last thing tough; I'm having a bit of trouble implementing the scheduler into my main while true loop.
When Main() is called, I created the scheduler and then it should run BAXTER() with schedule_reminder(scheduler) running in the background, not affecting BAXTER(). The problem I have is that the scheduler blocks BAXTER() and I can't run it properly.
#------------------------------------------------------------------------------------------
#                                   Run The Program
#------------------------------------------------------------------------------------------
def Main():
    #Create Schedule
    scheduler = sched.scheduler(time.time, time.sleep)
    StartupText()
    wishMe()
    speak("How may I be of service?") 
    while True:
        #Run Reminders
        schedule_reminder(scheduler)
        BAXTER()
        while not scheduler.empty():
            scheduler.run(False)
#------------------------------------------------------------------------------------------
How do I orient that portion of the code so I can run BAXTER() as intended with out the scheduler interfering/blocking it (Do I need threading??).

Thanks again.

Full Code:
#-------------------------------------------------------------------------------------
#                                  Imports
#-------------------------------------------------------------------------------------
import json
import random
import datetime
import operator
import os
import subprocess
import time
import sys
import sched
import webbrowser
import requests
from bs4 import BeautifulSoup
import wikipedia
import wolframalpha


from BX_Intents import (greetings, farewell, thanks, noAnswer, youChoose)
from BX_External_Functions import (autoTypeAnimation, StartupText, ShutdownText, 
                                    UserInput, listen, speak, getRandomJoke, getFunFacts,
                                    setReminders, setTodo, terminateTimers, sendEmail,
                                    wishMe, setRenewal, ErrorMsg, offlineWarningMsg,
                                    schedule_reminder)

# Print a warning msg if there is no internet to prevent pywhatkit 
# from crashing the program due to no connection 
try:
    import pywhatkit
except:
    offlineWarningMsg()
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
#                                       Main
#-------------------------------------------------------------------------------------
def BAXTER():
    command = UserInput() #Take user's input from terminal
    command=str(command).lower() #Convert user's input to lowercase

    #-------------------------------------------------------------------------------------
    #                       General Conversation (From Intents.py) 
    #-------------------------------------------------------------------------------------
    #Greetings
    patterns, responses = greetings()
    if (command in patterns):
        results = (random.choice(responses))
        autoTypeAnimation(results)
        speak(results)

    #Farewell
    patterns, responses = farewell()
    if (command in patterns):
        results = (random.choice(responses))
        autoTypeAnimation(results)
        speak(results)
    
    #Thanks
    patterns, responses = thanks()
    if (command in patterns):
        results = (random.choice(responses))
        autoTypeAnimation(results)
        speak(results)
    
    #No Response
    patterns, responses = noAnswer()
    if (command in patterns):
        results = (random.choice(responses))
        autoTypeAnimation(results)
        speak(results)

    #-------------------------
    #       Tell a Joke
    #-------------------------
    if ('joke' in command):
        try:
            joke = getRandomJoke()
            autoTypeAnimation(joke)
            speak(joke)
        except:
            ErrorMsg("get", "jokes")
    #-------------------------
    #-------------------------
    #     Tell a Fun Fact
    #-------------------------
    if ('fact' in command):
        try:
            funFact = getFunFacts()
            autoTypeAnimation(funFact)
            speak(funFact)
        except:
            ErrorMsg("get", "fun facts")
    #-------------------------
    
    #-------------------------------------------------------------------------------------
    #                       Search Wikipedia (General Info)
    #-------------------------------------------------------------------------------------
    if ('weather' not in command):
        if ('who is' in command) or ('what is the' in command) or ('what is a' in command) or ("what is" in command):
            if ('time' not in command):
                if ('news' not in command):
                    autoTypeAnimation('Searching Wikipedia...')
                    speak('Searching...')
                    command = command.replace("who is","")
                    command = command.replace("what is the","")
                    command = command.replace("what is a","")
                    command = command.replace("what is","")
                    try:
                        results = wikipedia.summary(command, sentences = 2)
                        autoTypeAnimation(results)
                        speak(results) 
                    except:
                        ErrorMsg("connect to", "Wikipedia")
                    
    #-------------------------------------------------------------------------------------
    #               Search Wolfram Alpha (Math/Conversions, Definitions)
    #-------------------------------------------------------------------------------------
    if ('news' not in command):
        if ('weather' in command) or ('calculate' in command) or ("what's" in command) or ('define' in command) or ("what" in command):
                autoTypeAnimation('Searching Wolfram Alpha...')
                speak('Searching...')
                command = command.replace("calculate","")
                command = command.replace("what's","")
                command = command.replace("define","")
                # Wolframalpha App Id
                appId = 'JH9XHR-W9J76L7H5A'
                try:
                    # Wolfram Instance
                    client = wolframalpha.Client(appId)
                    res = client.query(''.join(command))
                    results = next(res.results).text
                    autoTypeAnimation(results)
                    speak(results)
                except:
                    ErrorMsg("connect to", "Wolfram Alpha database")

    #-------------------------------------------------------------------------------------
    #                       Open Stuff on the Internet
    #-------------------------------------------------------------------------------------
    #Open Youtube Videos (Ex: 'Play __ on youtube')
    if ('youtube' in command):
        autoTypeAnimation("Launching Youtube...")
        speak('Launching Youtube')
        command = command.replace("youtube","")
        try:
            pywhatkit.playonyt(command)
        except:
            ErrorMsg("connect to", "Youtube")

    #Open Google Maps and Find The Location of A You Want
    if ('where is' in command):
        command = command.replace("where is","")
        autoTypeAnimation("Locating" + command + "...")
        speak('Locating' + command)
        webbrowser.open_new_tab("https://www.google.com/maps/place/" + command)

    #Search Stuff on Google
    if ('search' in command):
        command = command.replace("search", "")
        autoTypeAnimation("Searching" + command + " on Google")
        speak('Searching' + command)
        try:
            pywhatkit.search(command)
        except:
            ErrorMsg("connect to" , "Google")
    
    #Close Firefox
    if ('close firefox' in command):
        autoTypeAnimation("Terminating Firefox...")
        speak('Closing Firefox')
        command = command.replace("close firefox", "")
        browser = "firefox.exe"
        try:
            os.system("taskkill /f /im " + browser)   
        except:
            ErrorMsg("close", "Firefox")

    #-------------------------------------------------------------------------------------
    #                       Open Stuff on the Computer
    #-------------------------------------------------------------------------------------
    #Open Windows Media Player and Auto Play the Playlist Called Music
    if ('play music' in command) or ('media player' in command) or ('drop the needle' in command):
        autoTypeAnimation("Launching music...")
        speak("Launching Music")
        command = command.replace("play music", "")
        command = command.replace("media player", "")
        command = command.replace("drop the needle", "")
        try: 
            subprocess.Popen("C:\Program Files (x86)\Windows Media Player\wmplayer.exe /Playlist Music")
        except:
            ErrorMsg("open","Windows Media Player")

    #Close Windows Media Player
    if ('stop music' in command):
        autoTypeAnimation("Terminating music...")
        speak('Closing Music')
        command = command.replace("stop music", "")
        mediaPlayer = "wmplayer.exe"
        try:
            os.system("taskkill /f /im " + mediaPlayer)
        except:
            ErrorMsg("close", "Windows Media Player")

    #-------------------------------------------------------------------------------------
    #                         Set Reminders & Renewals
    #-------------------------------------------------------------------------------------
    if ('remind me' in command) or ('reminder' in command) or ('renew' in command):
        command = command.replace("remind me to", "")
        #If renew is mentioned in the command call the setRenewal Function
        if ('renew' in command):
            setRenewal()
        #Else, call the setReminders Function
        else:
            setReminders() #Call setReminders() from External Functions

    #-------------------------------------------------------------------------------------
    #                               Set ToDo
    #-------------------------------------------------------------------------------------
    if ('todo' in command):
        command = command.replace("add", "")
        command = command.replace("to the todo list", "")
        setTodo(command) #Call setTodo() from External Functions

    #-------------------------------------------------------------------------------------
    #                               Send E-Mails
    #-------------------------------------------------------------------------------------
    if ('email' in command):
        command = command.replace("email", "")
        sendEmail() #Call send E-Mail function fro External Functions
    
    #-------------------------------------------------------------------------------------
    #                       Stop Program/Script Command
    #-------------------------------------------------------------------------------------
    if ('stop' in command) or ('shutdown' in command) or ('quit' in command):
        speak("Shutting Down...")
        results = "Terminating program..."
        autoTypeAnimation(results)
        ShutdownText()
        terminateTimers()
        exit()
    #-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
      

#------------------------------------------------------------------------------------------
#                                   Run The Program
#------------------------------------------------------------------------------------------
def Main():
    #Create Schedule
    scheduler = sched.scheduler(time.time, time.sleep)
    StartupText()
    wishMe()
    speak("How may I be of service?") 
    while True:
        #Run Reminders
        schedule_reminder(scheduler)
        BAXTER()
        while not scheduler.empty():
            scheduler.run(False)
#------------------------------------------------------------------------------------------
Reply


Messages In This Thread
Help adding a loop inside a loop - by Extra - Oct-09-2022, 09:37 PM
RE: Help adding a loop inside a loop - by Extra - Oct-09-2022, 10:41 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 02:36 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 03:01 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 05:07 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 06:38 PM
RE: Help adding a loop inside a loop - by Extra - Oct-10-2022, 07:31 PM
RE: Help adding a loop inside a loop - by Extra - Oct-15-2022, 03:52 PM
RE: Help adding a loop inside a loop - by Extra - Oct-15-2022, 07:10 PM
RE: Help adding a loop inside a loop - by Extra - Oct-15-2022, 11:12 PM
RE: Help adding a loop inside a loop - by Extra - Oct-16-2022, 12:17 AM
RE: Help adding a loop inside a loop - by Extra - Oct-16-2022, 06:10 PM
RE: Help adding a loop inside a loop - by Extra - Oct-17-2022, 10:41 PM
RE: Help adding a loop inside a loop - by Extra - Oct-17-2022, 11:23 PM
RE: Help adding a loop inside a loop - by Extra - Oct-19-2022, 01:01 AM
RE: Help adding a loop inside a loop - by Extra - Oct-23-2022, 12:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning data on button click by buttons created by a loop bradells 3 394 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 2,304 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 1,636 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 721 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 742 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  Error loop with Chatgpt sportak12 0 525 Jan-14-2025, 12:04 PM
Last Post: sportak12
  How to convert while loop to for loop in my code? tatahuft 4 855 Dec-21-2024, 07:59 AM
Last Post: snippsat
  How to get keep information in a loop ginod 4 911 Dec-11-2024, 01:32 AM
Last Post: deanhystad
  Regarding The For Loop Hudjefa 5 1,500 Nov-15-2024, 01:02 PM
Last Post: deanhystad
  For Loop beginner agoldav 2 748 Nov-05-2024, 12:51 AM
Last Post: agoldav

Forum Jump:

User Panel Messages

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