0

I want to read a file line by line. These lines contain operators and numbers. If it's a "+" i will add the number that follows, it it's a "-" i will subtract the number that follows. This will be kept in a count variable. This count variable will then be stored in a list. I will continue to loop through the file until a duplicate count value is found. My idea is to continue to loop until i find a duplicate (hence my while condition). But when I get to the end of the file i need to start over!! So the count at the end of the file will now start from the beginning

While loop and then a for loop

def computeProblemOnePointFive(inputFileString):
    inputFile = open(inputFileString, "r")
    count = 0
    countsList=[]
    while len(countsList) == len(set(countsList)):
        for line in inputFile:
            if "+" in line:
                value = int(line.strip("+"))
                count = count + value
                countsList.append(count)
            if "-" in line:
                value = int(line.strip("-"))
                count = count - value
                countsList.append(count)
    return countsList[-1]
  • ...Can you be more specific about what exactly isn't working in your code? Is there a specific part of it that isn't working? Can you provide an example/actual input/output? – Green Cloak Guy May 12 '19 at 01:32
  • The code doesn't stop. I added print statements and then what i see is the for loop looping through the entire file and then when it gets to the end it leaves the for loop but never re-enters. it then just sits there because the while condition never turns to false. i know this problem will not go to false until it loops through the file a few more times – Brian Gurka May 12 '19 at 01:35
  • Isn't this a case of [re-read an open file](https://stackoverflow.com/questions/17021863/re-read-an-open-file-python) ? – Diane M May 12 '19 at 01:43
  • Try putting `inputFile.seek(0)` after the end of the inner `for` loop but still inside the `while` loop – Green Cloak Guy May 12 '19 at 01:44
  • Possible duplicate of [Re-read an open file Python](https://stackoverflow.com/questions/17021863/re-read-an-open-file-python) – Diane M May 12 '19 at 01:46
  • Or use `readlines` to store the lines, which may be faster and clearer. The only change necessary is to change `open(inputFileString, "r")` to `open(inputFileString, "r").readlines()`. However, you may want to change you code to use a `with` block to close the file. – iz_ May 12 '19 at 01:48

1 Answers1

0

A potential solution might be moving

inputFile = open(inputFileString, "r")

into the while loop

def computeProblemOnePointFive(inputFileString):
    # the line was here previously
    count = 0
    countsList=[]
    while len(countsList) == len(set(countsList)):
        inputFile = open(inputFileString, "r") #the line that was moved
        for line in inputFile:
            if "+" in line:
                value = int(line.strip("+"))
                count = count + value
                countsList.append(count)
            if "-" in line:
                value = int(line.strip("-"))
                count = count - value
                countsList.append(count)
    return countsList[-1]
David
  • 138
  • 1
  • 9