TimNeedsHalp
TimNeedsHalp

Reputation: 31

Endless loop with two loop functions

I want to know if my code is okay. The code is working and doing exactly what I want, printing out hello when in_position is true and printing world when in_position = False. I now want to fill these functions with real code. But I'm a beginner and prefer to ask, maybe unexpected errors occur there, or it is just unclean when filling with a lot of code.

import time

in_position = True

def loop():
    global in_position
    while in_position == True:
        time.sleep(2.4)
        print("Hello") 
        #fill with real code
        in_position = False
        loop_two()
        
        
def loop_two():
    global in_position
    while in_position == False:
        time.sleep(2.4)
        #fill with real code
        print("World")
        in_position = True
        loop()
        
    
    
def main():
    loop()
    loop_two()
    
main()

Upvotes: 1

Views: 85

Answers (2)

Grysik
Grysik

Reputation: 846

From what you have written in your comment

I want to fill the first loop with a web scraper for stock data and the second loop is to process these data.

I would suggest you to do something different in your code, and take a look into generators

You may rewrite it this way:

# instead of loop_one
def scrape_web():

    scraped_data = your_scraping_function()
    while scraped_data:
        yield scraped_data
        scraped_data = your_scraping_function()

#instead of loop_two    
def process_data(data):
    # here you put your processing algorithm
    processed_data = do_something_with(data)
    
def main():
    for new_data in scrape_web():
        process_data(new_data)

This should be much more memory efficient, and IMO cleaner solution. What this code exactly does:

  1. Calls scrape_web function, and when this function yields new data (you can think of yield as similar to return) then it is saved to new_data variable
  2. Calls process_data, which is handed new_data which you scraped earlier
  3. After processing data it will again call scrape_web, starting from line after yield - so it will call your_scraping_function from while loop, and if your_scraping_function return something (e.g. that is not None, False, empty string) then it will again yield this data, save it to new_data, and go back to point 2.

I know this is not exactly answering your first question regarding loops, but based on your comment I thought it might be useful to think different about this particular issue.

Upvotes: 0

Jules Civel
Jules Civel

Reputation: 641

Main calls loop() that then calls loop_two() that calls loop() ... and it goes again and again in an endless loop calls. You should put some ending conditions unless you want your code to run forever.

That also means that loop_two() is never called in main() (nor anything written after the first loop() call will be).

Another thing : use of global for the in_position parameter here doesn't look necessary. It should be more readable to put in_position as a parameter of your functions. Something like this :

import time

def loop(in_position ):
    while in_position == True:
        time.sleep(2.4)
        print("Hello") 
        #fill with real code
        in_position = False
        loop_two(in_position )
        
        
def loop_two(in_position ):
    while in_position == False:
        time.sleep(2.4)
        #fill with real code
        print("World")
        in_position = True
        loop(in_position )
        
    
    
def main():
    in_position = True
    loop(in_position)
    
main()

Upvotes: 2

Related Questions