Help debugging code written in python to get the output from the attached image, def main(): # set the initial month and declaration month = 1 old_adults = 0 old_babies = 0 adults = 1 # initial adult pair babies = 0 # initial babies pair total = 0 # storing the total number of rabbit pairs max = 500 # maximum number of rabbit pairs that can be accomodated # output file file = open("rabbit_sim.txt",'w') # header of output file file.write("# Table of rabbit pairs\n") file.write("Month, Adults, Babies, Total\n") # calculate the total number of rabbit pairs for the month total = adults+babies # loop that continues till total rabbit pairs < max while total < max: # output the counts to file file.write(str(month)+", "+str(adults)+", "+str(babies)+", "+str(total)+"\n") month += 1 # increment the month # set the old_adults and old_babies to current adults and babies old_adults = adults old_babies = babies # calculate the adults and babies for next month babies = adults adults = old_adults + old_babies total = adults+babies # calculate the total number of rabbit pairs for the month # output the counts when the total rabbit pairs > 500 file.write(str(month)+", "+str(adults)+", "+str(babies)+", "+str(total)+"\n") # output the month number when it runs out of cages file.write("# Cages will run out in month "+str(month)) # close the file file.close() if __name__ == '__main__': main() #excucte main function
Help debugging code written in python to get the output from the attached image,
def main():
# set the initial month and declaration
month = 1
old_adults = 0
old_babies = 0
adults = 1 # initial adult pair
babies = 0 # initial babies pair
total = 0 # storing the total number of rabbit pairs
max = 500 # maximum number of rabbit pairs that can be accomodated
# output file
file = open("rabbit_sim.txt",'w')
# header of output file
file.write("# Table of rabbit pairs\n")
file.write("Month, Adults, Babies, Total\n")
# calculate the total number of rabbit pairs for the month
total = adults+babies
# loop that continues till total rabbit pairs < max
while total < max:
# output the counts to file
file.write(str(month)+", "+str(adults)+", "+str(babies)+", "+str(total)+"\n")
month += 1 # increment the month
# set the old_adults and old_babies to current adults and babies
old_adults = adults
old_babies = babies
# calculate the adults and babies for next month
babies = adults
adults = old_adults + old_babies
total = adults+babies # calculate the total number of rabbit pairs for the month
# output the counts when the total rabbit pairs > 500
file.write(str(month)+", "+str(adults)+", "+str(babies)+", "+str(total)+"\n")
# output the month number when it runs out of cages
file.write("# Cages will run out in month "+str(month))
# close the file
file.close()
if __name__ == '__main__':
main() #excucte main function
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images