Alex Borland
Alex Borland

Reputation: 47

Python nested for loop only running once, then skips the rest of the iterations

I want this code to get 15 times the amount of data it got. When it runs, the first time it goes into all of the for loops, and then prints out the answer, but when it goes to loop the second time in the "for num in range(0,15):" loop, it goes into the "for df in page:" and then doesn't go any deeper into the other loops and just returns the "page_counter" incrementing by 200 as it should.

What should I do to make it run the final nested for loop every time the "for num in range(0,15):" loops?

 import pandas as pd

 symbol = 0
 for num in range(symbol,1):
     page_counter =0
     right = 1
     down = 0
     for num in range(0,15):
         page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan+1%2C+2005&enddate=Mar+2%2C+2017&num=200&ei=Aom4WPizNcWO2AaSnZ3YBg'+ str(page_counter), attrs = {'class': 'gf-table'}, header=0)

         for df in page:
             for num in range(down,200):
                 open_data = df.at[down, 'Open']
                 high_data = df.at[down, 'High']
                 low_data = df.at[down, 'Low']
                 close_data = df.at[down, 'Close']
                 #volume_data = df.at[down, 'Volume']

                 print(open_data)

                 down += 1
         page_counter += 200
         print(page_counter)
      symbol += 1

P.S. I know my code is a bit janky right now, this is just a quick idea I had.

Upvotes: 0

Views: 426

Answers (1)

user7651652
user7651652

Reputation:

Declare down = 0 inside for num in range(0,15):

after 1st run down = 200 so this loop not satisfying for num in range(down,200): range condition.

 import pandas as pd

 symbol = 0
 for num in range(symbol,1):
     page_counter =0
     right = 1
     for num in range(0,15):
         down = 0
         page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan+1%2C+2005&enddate=Mar+2%2C+2017&num=200&ei=Aom4WPizNcWO2AaSnZ3YBg'+ str(page_counter), attrs = {'class': 'gf-table'}, header=0)

         for df in page:
             for num in range(down,200):
                 open_data = df.at[down, 'Open']
                 high_data = df.at[down, 'High']
                 low_data = df.at[down, 'Low']
                 close_data = df.at[down, 'Close']
                 #volume_data = df.at[down, 'Volume']

                 print(open_data)

                 down += 1
         page_counter += 200
         print(page_counter)
      symbol += 1

Upvotes: 3

Related Questions