I am trying to parse a text file, from then I can decide whether or not it is above the line y=x
, on, on or below. The code runs through, but is not executing my readline()
s.
My text file I am using is this:
4
2
1
3
3
5
6
7
8
I have altered the code by doing different forms of the readline()
.
if res == "yes":
with open('text.txt', 'r') as f:
alist = [line.rstrip() for line in f]
timesgo = 0
terms = f.readline()
while str(terms) > str(timesgo):
temp = f.readline()
y = f.readline()
if temp < y:
u = str(temp) + "," + str(y)
Above.append(u)
timesgo += 1
elif temp == y:
u = str(temp) + "," + str(y)
On.append(u)
timesgo += 1
if temp > y:
u = str(temp) + "," + str(y)
Below.append(u)
timesgo += 1
I expect to have 4 coordinates in total, in the case of the file: (2,1)
, (3,3)
, (5,6)
, (7,8)
should appended to the appropriate list to then be sorted in by further code.
My version:
if res == "yes":
with open('text.txt') as f:
terms = f.readline()
for _ in range(int(terms)):
x = int(f.readline())
y = int(f.readline())
#u = (x, y) # as tuple with numbers
u = "{},{}".format(x, y) # as string
if x < y:
Above.append(u)
elif x > y:
Below.append(u)
else
On.append(u)
Your mistake was
alist = [line.rstrip() for line in f]
which was reading all lines from file and later readline()
had nothing to read.
The other changes are only cosmetic.