Welcome, guest | Sign In | My Account | Store | Cart

After wondering how human populations can increase in size and writing a simulator for the problem, a simpler question came to mind regarding a population without death. This creature population simulator is an old experiment that served its purpose well. The code here is committed for archival to be run under Python 2.5 or later versions.

Python, 93 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93``` ```def main(): while True: creatures = get_creatures() years = get_years() summary = get_summary() target = get_target() simulate(creatures, years, summary, target) if get_exit(): return def get_creatures(): while True: try: creatures = int(raw_input('How many creatures do you want to start with? ')) if creatures > 0: return creatures print 'There must be at least one creature.' except: print 'You must enter a number.' def get_years(): while True: try: years = int(raw_input('How many years do you want simulated? ')) if years > 0: return years print 'At least one year must be simulated.' except: print 'You must enter a number.' def get_summary(): while True: try: answer = raw_input('Do you want a summary of the simulation? ').lower() if answer == 'yes': return True if answer == 'no': return False except: pass print 'You must enter "yes" or "no".' def get_target(): while True: try: target = int(raw_input('What is the target population (0 for None)? ')) if target > 0: return target return 0 except: print 'You must enter a number.' def simulate(creatures, years, summary, target): print divisions = [0 for division in range(21)] divisions = creatures for year in range(years): if not summary: print 'It is year', year, 'and the divisions are as follows:' print str(divisions)[1:-1] children = divisions / 2 divisions += divisions for division in range(19): divisions[19 - division] = divisions[18 - division] divisions = children if target: total = 0 for division in range(21): total += divisions[division] if total >= target: break print 'It is year', year + 1, 'and the divisions are as follows:' print str(divisions)[1:-1] total = 0 for division in range(21): total += divisions[division] print 'There are a total of', total, 'creatures.' print def get_exit(): while True: try: answer = raw_input('Do you wish to exit this program? ').lower() if answer == 'yes': return True if answer == 'no': return False except: pass print 'You must enter "yes" or "no".' if __name__ == '__main__': main() ``` Created by Stephen Chappell on Thu, 16 Aug 2012 (MIT)

### Required Modules

• (none specified)