0

I want to use 'number' variable that was created out of the function and use it in couple functions and also give it an initial value 0, but it gives "local variable 'number' referenced before assignment" error at the last line of code.How could I fix it?Thanks in advance.

lst_img=[img0,img1,img2,img3,img4]
number=0

def go_forward():

    global number
    number+=1
    shwimage.grid_forget()
    global shwimage1
    shwimage1=Label(image=lst_img[number])
    shwimage1.grid(row=0,column=0,columnspan=3)

def go_back():

   if number==0:
      shwimage.grid_forget()
   shwimage1.grid_forget()
   shwimage2=Label(image=lst_img[number-1])
   shwimage2.grid(row=0,column=0,columnspan=3)
   number-=1  # local variable 'number' referenced before assignment
  • Well you could use a global var, but not considered good practice. So you would need to pass that var to the other function, one way or another – SuperStew Feb 11 '22 at 16:41
  • You didn't use `global number` in `go_back`. If you are going to go the `global` route you need to include your `global` variables in EVERY function that uses them. – OneMadGypsy Feb 11 '22 at 16:44
  • Does this answer your question? [Local (?) variable referenced before assignment](https://stackoverflow.com/questions/11904981/local-variable-referenced-before-assignment) – luk2302 Feb 11 '22 at 16:44
  • 1
    Thanks Micheal Guidry, your comment helped to solve the problem.I am new here and am surprised how fast people can answer the questions, thanks everyone. – jemmitbliss Feb 11 '22 at 16:53
  • @jemmitbliss I think it is also useful to read for you: https://betterprogramming.pub/alternatives-to-using-globals-in-python-a3b2a7d5411b – Park Feb 11 '22 at 16:59

6 Answers6

0

You have to also tell go_back() to use the global variable:

def go_back():
   global number #add this
   if number==0:
      shwimage.grid_forget()
   shwimage1.grid_forget()
   shwimage2=Label(image=lst_img[number-1])
   shwimage2.grid(row=0,column=0,columnspan=3)
   number-=1  # local variable 'number' referenced before assignment

global tells the function to use the global number so each function has to be told this separately or it will default to using the local version.

Eli Harold
  • 2,280
  • 1
  • 3
  • 22
0

A reason you get the error, local variable 'number' referenced before assignment, is you did not used global keyword to declare that you will use THE number variable even in go_back().

A simple solution is to use global in the go_back(), as follows:

def go_back():
   global number # -> as you did in `go_forward()`.
   if number==0:
      shwimage.grid_forget()
   shwimage1.grid_forget()
   shwimage2=Label(image=lst_img[number-1])
   shwimage2.grid(row=0,column=0,columnspan=3)
   number-=1  # local variable 'number' referenced before assignment

However, you need to play it safe when using global because it makes hard to track variable changes. Instead, it would be better to pass number value as an argument in the functions.

For more information to use global keyword, see an answer of the question here: Why are global variables evil?

In addition, I recommend you to read this article, Alternatives to Using Globals in Python

Park
  • 2,446
  • 1
  • 16
  • 25
0

I believe in go_back() function also, you are trying to reference the global variable number But it is not instructed to the function to use global number variable.

def go_back():
   global number #This line needs to be addded
   if number==0:
      shwimage.grid_forget()
   shwimage1.grid_forget()
   shwimage2=Label(image=lst_img[number-1])
   shwimage2.grid(row=0,column=0,columnspan=3)
   number-=1
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kirti
  • 1
  • 1
0

The others answers' solution to add global number in go_back is working, but using global variables is really not a good practice when you can do otherwise.

What you could do here is use object oriented programming. Create a class with a number, lst_img and showimage properties and two class methods, go_forward and go_backward, that can modifiy the number and showimage properties. How I would start :

class ImageViewer:
    # Class constructor
    def __init__(self, lst_img):
        self.lst_img = lst_img
        self.number = 0 # number is always 0 at the beginning
        self.showimage = None

    def go_forward(self):
        if self.number == len(self.lst_img)-1 :
            return
        else:
            self.number += 1

        # ... add your code ....
        if self.showimage is None:
            # code to create a showimage...
            self.showimage = .....
        else:
            # code to update the showimage...

    def go_backward(self):
        if self.number == 0:
            return
        else :
            self.number -= 1

        if self.showimage is None:
            # code to create a showimage...
            self.showimage = .....
        else:
        # code to update the showimage...

# Create an viewer instance with a list of images
lst_img  = [img1, img2, img3]
viewer = ImageViewer(lst_img)

# Now you can use the go_forward and go_backward methods to naviguate in your images
viewer.go_forward()
viewer.go_forward()
viewer.go_backward()
....
Dharman
  • 30,962
  • 25
  • 85
  • 135
yjn
  • 103
  • 1
  • 7
-1

The problem is that you are using the number variable as global variable.

  • this is a comment, not an answer – SuperStew Feb 11 '22 at 16:43
  • This is not an answer. – OneMadGypsy Feb 11 '22 at 16:46
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 11 '22 at 18:29
-1

Use global number in both of your functions:

lst_img=[img0,img1,img2,img3,img4]

number=0

def go_forward():

    global number
    number+=1
    shwimage.grid_forget()
    global shwimage1
    shwimage1=Label(image=lst_img[number])
    shwimage1.grid(row=0,column=0,columnspan=3)

def go_back():
    
   global number
   if number==0:
      shwimage.grid_forget()
   shwimage1.grid_forget()
   shwimage2=Label(image=lst_img[number-1])
   shwimage2.grid(row=0,column=0,columnspan=3)
   number-=1
Matei Piele
  • 572
  • 2
  • 15