-1

I have a text file I'm reading from and just threw a counter on there to make sure I grabbed everything but when I implemented a simple counter it acted weird. It works now but I had to do the following:

f = open("street.txt", "r")
l = ""
count = -1
for line in f:
    if(line[0].isdigit()):
        l = line.replace('\n', '')

    else:
        count=count+1
        l = l + " " + line.replace('\n', '')
        c = str(count) + ')'
        print(c + l + '\n')

Essentially I was attempting to just run through this file, add every other line to the previous line then number them just as a check with a count variable, that I covered everything. For some reason when the count was running it started at 2 when I had count initially set to 0. It wouldn't print out "1)" until I changed count to -1 initially. That print statement is an L, not a 1. I have no idea why it was doing that. I didn't get an error, it ran fine just with the wrong numbers for about 4-5 runs. And that's an L in the print statement...which should be fairly obvious from the if statement but just in case.

John Verber
  • 745
  • 2
  • 16
  • 31
  • 3
    What are you trying to do here? What do you want to happen, and what result are you getting instead?. This isn't a [mcve], so it's hard to see what you are asking. – juanpa.arrivillaga Apr 15 '18 at 20:32
  • So what are you seeing from this code and what did you expect? – khelwood Apr 15 '18 at 20:32
  • 2
    I am not sure what your trying to do, all I want to ask is, are you not receiving an error when you run this code?, because first you declare `c = str(count) + ')'` and then next you say `print(c + l + '\n')`, I am pretty sure you can't add `1` to a string. i.e. `c+1` should cause an error. – SShah Apr 15 '18 at 20:52
  • I believe that there may have been a memory issue someplace on my machine cause it works like a charm now. Essentially I was attempting to just run through this file, add every other line to the previous line then number them just as a check that I covered everything. For some reason when the count was running it started at 2 when I had count initially set to 0. It wouldn't print out "1)" until I changed count to -1 initially. That print statement is an L, not a 1. I have no idea why it was doing that. I didn't get an error, it ran fine just with the wrong numbers for about 4-5 runs. – John Verber Apr 16 '18 at 00:04

1 Answers1

0

Just initialize count with 0 and increment statement should be in the last.

l = l + " " + line.replace('\n', '') 
c = str(count) + ')' 
print(c + l + '\n')
count+=1
Hassan Anwer
  • 347
  • 2
  • 14
  • Yeah as I said initially I did that and it came out starting at 2. I think there must've been a memory issue cause I just ran it again and it worked just fine.... – John Verber Apr 15 '18 at 21:13