Python (2.7) Operation on a closed file error
Question: Another user here recently helped me refine the code below. I have the sorted
list of the integers, but I don't know how to get the original list since the
with statement closes the file it is reading from. I have already returned the
variable "List" and have a print statement for it, if I can just find a way to
produce it. I thought it would be easy enough to use the readlines fuction,
but I can't figure out how to do that. Thanks for any help! I am still new to
Python...

    
    
    import random
    with open("1000.txt", "w") as f:
        for x in range(1000):
            f.write(str(random.randint(0, 9999))+"\n")
    
    def readList():
        infile = raw_input("Input File Name: ")
        rawList = list()    
        with open(infile, 'r') as infi:
            for line in infi:
                rawList.append(int(line))
        sortedList = sortList(rawList) #function call for sorting the list, returned sorted list is stored
        n = int(raw_input("Which number do you want to return?: "))
        user_num = int(float(n-1))
        return sortedList, user_num, List
    
    def sortList(inpList): #function needs list as input and returns sorted list
        inpList.sort() 
        return inpList
    
    if __name__=='__main__': #This is how the main is defined in python
        sortedList, List, user_num = readList() #function call of readList, returned two objects are stored in variables
        first = sortedList[0] 
        lines = len(sortedList)
        orig_min_val = min(List)
        orig_max_val = max(List)
        minimum_val = min(sortedList)
        maximum_val = max(sortedList)
        if user_num > 1000:
            print user_num, "is greater than 1000!" # you don't need brackets when printing
        elif user_num > lines:
            print "There aren't that many numbers in the list!"
        elif lines < 1000:
            print "WARNING: only", lines, "numbers were read into the list!"
        print "Before sort:"
        print "Min is", orig_min_val
        print "Max is", orig_max_val
        print "After sort:"
        print "Min is", minimum_val
        print "Max is", maximum_val
        print user_num
    


Answer: Spelling and ordering are VERY important in programming. You have a couple
small blunders.

First, this line:

    
    
    return sortedList, user_num, List
    

Is returning something called `List`. However, you don't actually have a
variable called `List` anywhere in that method; what you're actually returning
is a reference to the python class `List` (Notice how the syntax highlighting
in your post colors it blue? That's why). You probably want to instead return
`rawList` like this:

    
    
    return sortedList, user_num, rawList
    

Second, this line:

    
    
    sortedList, List, user_num = readList()
    

Is getting the results back from that return. If you look closely, you'll see
that they're actually in the wrong order. Not only that, but you are assigning
one of the values to the python class `List`! This is very bad. I recommend
you rename your variable in the `__main__` method from `List` to `rawList`,
and make sure your variables are correctly ordered like so:

    
    
    sortedList, user_num, rawList = readList()
    

