0

So I have this piece of code to access number pairs from a file and sort them in respective arrays:

Input: 10 6 5

Content of fInp.txt:

3 7
4 5
2 7
9 10
1 3
1 8
4 2
7 10
5 3
2 5
8 6

The code:

[m,n,p] = [int(i) for i in input().split()]
with open('fInp.txt') as file:
    v = file.readlines()
def inpint(h):
    ar = []
    for i in v:
        q = h
        j = [int(j) for j in i.split()]
        if j[0] > j[1]:
            j = [j[1],j[0]]
        ar.append(j)
        q+=1
        if q == h:
            break
    v = v[h:]
    ar.sort(reverse=False,key=lambda k: k[0])
    return ar
a,b = map(inpint,(n,p))
print(a,b)

This piece of code does not invoke any exceptions, however it's logically incorrect since I want the 2 lists to take up diff number pairs. And so I added this line right above 'return' to make the function not iterate the first number pairs twice:

v = v[h:]

The code snippet now provokes an UnboundLocalError. How did this happen and how do I fix it?

enzo
  • 9,121
  • 2
  • 12
  • 36
htrd
  • 1

0 Answers0