I Created this code but i keep getting returned the error: ValueError: I/O operation on closed file What can I do to correct this mistake?
def eightBitStrings(n):
if n > 0:
#Assign variable to reference the name of the file... This shall be called EightBits...
eightBits = "eightBits.txt"
#Open file above
outputFile = open(eightBits, "w")
#Track the refrencenumber of values that we generate.
#As we generate values, we will decrement the value of counter
counter = 0
#As long as counter is LESS than n, we will need more numbers
# Input random number, convert it to a string and then write it to the file on its own line
#Add 1 to the counter since have generated a number
while (counter < n):
randomNumber = int(random.random() * 1001)
outputFile.write(str(randomNumber) + "\n")
counter = counter + 1
outputFile.close()
Any suggestion would be appreciated