Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:37:57+00:00 2026-05-10T22:37:57+00:00

I am working on Conway’s Game of Life currently and have gotten stuck. My

  • 0

I am working on Conway’s Game of Life currently and have gotten stuck. My code doesn’t work.

When I run my code in GUI, it says:

  [[0 0 0 0]  [0 1 1 0]  [0 1 0 0]  [0 0 0 0]]  Traceback (most recent call last):   File 'C:\Users\Documents\Physics\Python\MainProject\conway.py', line 53, in      b= apply_rules(a)   File 'C:\Users\Documents\Physics\Python\MainProject\conway.py', line 14, in apply_rules     neighbours=number_neighbours(universe_array,iy,ix)   File 'C:\Users\Documents\Physics\Python\MainProject\conway.py', line 36, in number_neighbours     neighbours+=1 UnboundLocalError: local variable 'neighbours' referenced before assignment 

Here is my code:

'''If a cell is dead at time T with exactly three live neighbours, the cell will be alive at T+1 If a cell is alive at time T with less than two living neighbours it dies at T+1 If a cell is alive at time T with more than three live neighbours it dies at T+1 If a cell is alive at time T with exactly two or three live neighbours it remains alive at T+1''' import numpy   def apply_rules (universe_array):     height, width = universe_array.shape     # create a new array for t+1     evolved_array = numpy.zeros((height, width),numpy.uint8)     for iy in range(1, height-1):         for ix in range(1,width-1):             neighbours=number_neighbours(universe_array,iy,ix)             if universe_array[iy,ix]==0 and neighbours==3:                 evolved_array[iy,ix]==1             elif universe_array[iy,ix]==1 and neighbours<2:                 evolved_array[iy,ix]==0             elif universe_array[iy,ix]==1 and neighbours>3:                 evolved_array[iy,ix]==0             elif universe_array[iy,ix]==1 and neighbours==2 or neighbours==3:                 evolved_array[iy,ix]=universe_array[iy,ix]      return evolved_array  def number_neighbours(universe_array,iy,ix):     neighbours=0 #fixed this line,thanks:)     if universe_array[iy-1,ix-1]==1:             neighbours+=1     if universe_array[iy,ix-1]==1:             neighbours+=1     if universe_array[iy+1,ix-1]==1:             neighbours+=1     if universe_array[iy-1,ix]==1:             neighbours+=1     if universe_array[iy+1,ix]==1:             neighbours+=1     if universe_array[iy-1,ix+1]==1:             neighbours+=1     if universe_array[iy,ix+1]==1:             neighbours+=1     if universe_array[iy+1,ix+1]==1:             neighbours+=1     else:         neighbours=neighbours     return neighbours  if __name__ == '__main__':     a = numpy.zeros((4,4),numpy.uint8)     a[1,1]=1     a[1,2]=1     a[2,1]=1     print a     b= apply_rules(a)     print b 

I am a beginner at Python, and I don’t know how to fix the error. I am a little bit confused about import 'neighbours' to function 'apply_rules', is that right way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:37:58+00:00Added an answer on May 10, 2026 at 10:37 pm

    Well, I guess that you are also quite new to programming per se, otherwise you should not have any problems in interpreting that simple error message.

    I’ll help you dissect it:

    • First, all ‘current’ line numbers of your project’s files are displayed, in calling order.
    • Then, it shows you the function in which the error occured: number_neighbours
    • Then, it shows you the contents of the line that contains the error: neighbours+=1
    • Finally, it tells you what the problem with that line is: UnboundLocalError: local variable 'neighbours' referenced before assignment

    Now, what does that mean? Let’s see what the += operator does: it adds something to the current value of neighbours. That means that it reads the current value, adds something to it, and finally stores it back. ‘Reading’ is called ‘reference’ with respect to variables.

    What is the current value of neighbours? Well, it has never been used before, so it doesn’t have any value — there has never been a value assigned to it. Adding something to ‘no value’ is not a sensible thing to do. I guess that you expect it to have the value 0, but you have to tell it to your interpreter. To do this, add the following statement before, at the start of your function: neighbours = 0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The getNormalizingTransform() method of the class GraphicsConfiguration looks like it… May 11, 2026 at 2:21 pm
  • added an answer This is really just a simple linear algebra problem, so… May 11, 2026 at 2:21 pm
  • added an answer The key is the difference in the AND [orders].[deptid] =… May 11, 2026 at 2:21 pm

Related Questions

I am working on Conway's Game of Life currently and have gotten stuck. My
Is it a good idea to quote keys when using a hash in Perl?
I am working on localization for a asp.net application that consists of several projects.
I am working on a web application using Python (Django) and would like to
I am working on an ASP.NET project which is physically located at C:\Projects\MyStuff\WebSite2. When

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.