Help with code?

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
It just outputs 1 loads of times

<What do you expect to happen instead?>
I want my code to loop through a file (which is saved as a .txt) and split it into sections which have the " : " after it. Then I want it to get all the numbers from column 6 to get subtract by column 5 and print it out afterward.

```python

count = 4
count2 = 5

numbers = open(“number.txt”, “r”)
number = numbers.split(’:’)

for lines in number:

number_1 = number[count]
number_2 = number[count2]

total = (int(number_1)) - (int(number_2))

print total

count = 3
count2 = 4
count3 = 5

numbers.close()

Example of file -

10 : 11 : 12 : 13 : 14 : 15 : 16 :

<do not remove the three backticks above>

you can’t add split directly on the file, you need to get to the lines first:

numbers = open("number.txt", "r")
number = numbers.readline()
print number.split(':')

or in your case, you have a loop:

numbers = open("number.txt", "r")
for lines in numbers:
   line = lines.split(':')
2 Likes

Thanks, I was about to ask for a loop

yea, i need to look up the loop syntax.

but the most important thing: you can’t call split directly on the file, you need the lines, split() is designed to work for strings, not files

Thank you, that helped me a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.