Python will not print to a text file
Question: For some reason, the following code is unable to print to `Cache.txt`

    
    
    import random
    import time
    
    text_file = open("Cache.txt", "w")
    text_file.write("Numbers Used \n")
    
    print("Welcome to the Bingo number generator")
    UserNumber = str((random.randint(1,90)))
    print(UserNumber)
    text_file.write ("UserNumber")
    text_file.close
    

Can someone help me figure out what is wrong with this code?


Answer: you need to call `close()` function like this:

    
    
    text_file.close()
    

Plus, a more pythonic way would be to call `open()` with context manager
[with](https://docs.python.org/2/library/contextlib.html) :

    
    
    with open("Cache.txt", "w") as text_file:
        text_file.write("Numbers Used \n")
        print("Welcome to the Bingo number generator")
        UserNumber = str((random.randint(1,90)))
        print(UserNumber)
        text_file.write ("UserNumber")
    

No need to call `close()` doing that way

