I’ve seen a bunch of similar questions to this with great answers but I’m not sure how to apply them to the following code. I am just learning and other than this error I think I am good to go on this assignment so any help would be much appreciated!
def do_research():
num_month = 1
num_cages = int(input("Number of cages: "))
num_adults = int(input("Adult pairs in the first month: "))
num_babies = int(input("Pairs of babies in the first month: "))
total = num_adults + num_babies
output_file = open("rabbits.csv", "w")
output_file.writelines("# Table of rabbit pairs\n")
output_file.writelines("Month, Adults, Babies, Total\n")
output_file.writelines(str(num_month) + ", ")
output_file.writelines(str(num_adults) + ", ")
output_file.writelines(str(num_babies) + ", ")
output_file.writelines(str(total) + "\n")
while (total / 2) < num_cages:
num_month += 1
num_babies = num_adults
num_adults += num_babies
total = num_adults + num_babies
output_file.writelines(str(num_month) + ", ")
output_file.writelines(str(num_adults) + ", ")
output_file.writelines(str(num_babies) + ", ")
output_file.writelines(str(total) + "\n")
output_file.writelines("# Cages will run out in month " + str(num_month))
output_file()
do_research()
>Solution :
The problem is with the second to last line of your code:
output_file()
That line doesn’t make sense. Just as the error message says, the object being referenced by output_file is not callable. It’a file handle…a reference to an open file. That isn’t something that is callable. Said another way, you can’t treat that object like it is a function or method. I assume that this is just a syntax error and you meant something else.
In the future, please supply the stack trace that accompanies the error message you are getting. I assume that in this case, the accompanying stack trace is pointing to this line of your code.