1

I want to execute a loop if and only 5 lines have been executed inside the text file that's being written to. The reason being, I want the average to be calculated from the final 5 lines of the text file and if the program doesn't have 5 numbers to work with, then a rumtime error is thrown.

    #Imports
    from bs4 import BeautifulSoup
    from urllib import urlopen
    import time

    #Required Fields
    pageCount = 1290429


    #Loop
    logFile = open("PastWinners.txt", "r+")
    logFile.truncate()
    while(pageCount>0): 
        time.sleep(1)
        html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
        soup = BeautifulSoup(html, "html.parser")

        try:
            section = soup.find('div', {"class":"row panel radius"})
            crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
            logFile.write(crashPoint[0:-1]+"\n")
        except:
            continue

        for i, line in enumerate(logFile):             #After 5 lines, execute this
            if i > 4:
                data = [float(line.rstrip()) for line in logFile]
                print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:])))
            else:
                continue

        print(crashPoint[0:-1])
        pageCount+=1
    logFile.close()

If anyone knows the solution, or knows a better way to go about doing this, it would be helpful, thanks :).

Edit:

Updated Code:

#Imports
from bs4 import BeautifulSoup
from urllib import urlopen
import time

#Required Fields
pageCount = 1290429
lineCount = 0

def FindAverage():
    with open('PastWinners.txt') as logFile:
        data = [float(line.rstrip()) for line in logFile]
        print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:])))

#Loop
logFile = open("PastWinners.txt", "r+")
logFile.truncate()
while(pageCount>0): 
    time.sleep(1)
    html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
    soup = BeautifulSoup(html, "html.parser")

    if lineCount > 4:
        logFile.close()
        FindAverage()
    else:
        continue

    try:
        section = soup.find('div', {"class":"row panel radius"})
        crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
        logFile.write(crashPoint[0:-1]+"\n")
    except:
        continue

    print(crashPoint[0:-1])
    pageCount+=1
    lineCount+=1


logFile.close()

New Problem: The program runs as expected, however once the average is calculated and displayed, the program doesn't loop again, it stops. I want it to work so after 5 lines it calculates the average and then displays the next number, then displays a new average and so on and so.

6
  • 1
    So, is your code working or not? If not, can you show an example of expected result and actual result? Commented Jul 21, 2016 at 13:25
  • Also, did you think of just checking if there were at least 4 end of line ? Or do you want to pass over blank lines ? Commented Jul 21, 2016 at 13:26
  • Can't you just count the lines written, and continue until the count is greater than 4? Commented Jul 21, 2016 at 13:28
  • @spectras Edited to show more information, as well as code changes Commented Jul 21, 2016 at 13:35
  • @martineau, Tried doing that, made it a lot easier to call the method, however it still isn't working as intended Commented Jul 21, 2016 at 13:42

2 Answers 2

0

Your while loop is never going to end. I think you meant to decrement: pageCount-=1.

Sign up to request clarification or add additional context in comments.

2 Comments

Good point, but doesn't explain the ZeroDivisionError in the updated code.
The loop is meant to go forever as I'm pulling data from a website constantly. I've managed to actually get this working now, however I have another problem which I'll add on now to the thread.
0

The problem at the end was that the loop wasn't restarting and just finishing on the first average calculation. This was due to the logFile being closed and not being reopen, so the program thought it and appending to the file, it works just as expected. Thanks to all for the help.

#Imports
from bs4 import BeautifulSoup
from urllib import urlopen
import time

#Required Fields
pageCount = 1290429
lineCount = 0

def FindAverage():
    with open('PastWinners.txt') as logFile:
        data = [float(line.rstrip()) for line in logFile]
        print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:])))

#Loop
logFile = open("PastWinners.txt", "r+")
logFile.truncate()
while(pageCount>0):
    time.sleep(1)
    html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read()
    soup = BeautifulSoup(html, "html.parser")

    try:
        section = soup.find('div', {"class":"row panel radius"})
        crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip()
        logFile.write(crashPoint[0:-1]+"\n")
    except:
        continue

    print(crashPoint[0:-1])
    pageCount+=1
    lineCount+=1

    if lineCount > 4:
        logFile.close()
        FindAverage()
        logFile = open("PastWinners.txt", "a+")
    else:
        continue
logFile.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.