Python Files, adding values to/from a list
Question: I am trying to add a value to a list from a file, and then be able to add the
values from the file to the list. This is what I have:

    
    
    L = []        
    def readFile(L):
        ui = input("Please enter your file name:")
        r = open(ui, 'r')
        files = r.readlines()
        for line in files:
            return float(line)
        L.append(line)
        r.close()
    
    def fileAddValue(L):
        ui = input("Please enter your file name:")
        val = float(input("Enter the value you would like to add to the list: "))
        r = open(ui, 'a')
        file = r.write(str(val) + '\n')
        for ix in r:
            x = float(ix)
        L.append(x)
        L.sort()
        r.close()
    


Answer: Do you need something like that?

    
    
    L = []
    def readFile():
        ui = input("Please enter your file name:")
        r = open(ui, 'r')
        files = r.readlines()
        for line in files:
            value = float(line)
            L.append(value)
        r.close()
    
    def fileAddValue():
        ui = input("Please enter your file name:")
        val = float(input("Enter the value you would like to add to the list: "))
        r = open(ui, 'a+')
        r.write(str(val) + '\n')
        for ix in r:
            x = float(ix)
            L.append(x)
        L.append(val)
        L.sort()
        r.close()
    
    if __name__ == '__main__':
        readFile()
        fileAddValue()
        print(L)
    

While it's non-pytonic (tried to not touch your code unless necessary), it
works if I got your question right. Indenting code is important in Python and
returning from a function guarantees that code after return will never run. If
you want a function that "returns" several values so you can iterate over that
"function" with `for`, use `yield` instead of `return`.

