Python Forum
Global variable does not seem to be global.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global variable does not seem to be global.
#1
I am trying to use a global variable which is then incremented in a function but I am getting this error:

File "E:\Python 3\Prehistoric Life\prehistoricLife.py", line 169, in fetchRecord recNo += 1
UnboundLocalError: local variable 'recNo' referenced before assignment.

It is saying that recNo is a local variable.

Here is an excerpt of the code where I set recNo as a global and then the function where I am using it.

from tkinter import *
from tkinter import ttk
import sqlite3

root = Tk()


#---------------------------[ Global Variables ]--------------------------------
dinoPath = 'dino_images\\'
sizePath = 'dinosize\\'
maxRec = 0
recNo = 0
rowCount = 0

#---------------------------- Clear all entry boxes ---------------------------------
def fetchRecord():
    recBox.delete(0,END)      #entry boxes
    eName.delete(0,END)      
    eMeaning.delete(0,END)
    ePronounce.delete(0,END)
    ePeriod.delete(0,END)
    eGroup.delete(0,END)
    eSize.delete(0,END)
    eLived.delete(0,END)
    eDiet.delete(0,END)
    eFossils.delete(0,END)
    factFile.delete(1.0,END)    #Textbox
    
#---------------------------- Get Record From Database ---------------------------------   
   recNo += 1     #increment recNo
    
    conn=sqlite3.connect('dinobase.db')
    c=conn.cursor()
    c.execute('SELECT * FROM dino WHERE record = recNo')
    rows=c.fetchall()
    
    for row in rows:
        recBox.insert(0,row[0])
        eName.insert(0,row[1])
        eMeaning.insert(0,row[2])
        ePronounce.insert(0,row[3])
        ePeriod.insert(0,row[4])
        eGroup.insert(0,row[5])
        eSize.insert(0,row[6])
        eLived.insert(0,row[7])
        eDiet.insert(0,row[8])
        eFossils.insert(0,row[9])
        factFile.insert(1.0,row[10])
    
    c.close()
    conn.close()
Any suggestions appreciated.
Reply
#2
You don't want to try to modify global variables within a function, it just leads to a mess that is hard to maintain. It is much better to use functions with parameters and return values:

a = 5
def foo(x)
    return x + 2
a = foo(a)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks. I need to increment the value of recNo and use it in that function to get the record. That is why I tried incrementing recNo within that function. I tried passing recNo as a parameter and then using local variable x=recNo, incrementing x and then recNo=x but have this error:

TypeError: fetchRecord() missing 1 required positional argument: 'recNo'

ef fetchRecord(recNo):
    recBox.delete(0,END)
    eName.delete(0,END)      #entry boxes
    eMeaning.delete(0,END)
    ePronounce.delete(0,END)
    ePeriod.delete(0,END)
    eGroup.delete(0,END)
    eSize.delete(0,END)
    eLived.delete(0,END)
    eDiet.delete(0,END)
    eFossils.delete(0,END)
    factFile.delete(1.0,END)    #Textbox
    
    x=recNo
    x += 1
    recNo = x
    
    conn=sqlite3.connect('dinobase.db')
    c=conn.cursor()
    c.execute('SELECT * FROM dino WHERE record = recNo')
    rows=c.fetchall()
    
    for row in rows:
        recBox.insert(0,row[0])
        eName.insert(0,row[1])
        eMeaning.insert(0,row[2])
        ePronounce.insert(0,row[3])
        ePeriod.insert(0,row[4])
        eGroup.insert(0,row[5])
        eSize.insert(0,row[6])
        eLived.insert(0,row[7])
        eDiet.insert(0,row[8])
        eFossils.insert(0,row[9])
        factFile.insert(1.0,row[10])
    
    c.close()
    conn.close()
Not the best way I guess.
Reply
#4
You don't need all that complication. Pass it as a parameter, update it in the function, return the updated value, assign the function call back to the original variable.

def fetchRecord(recNo):
    ...
    recNo += 1
    ...
    return recNo

recNo = fetchRecord(recNo)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thank you very much. Since my last post I was playing with it and I now have it working within the function as I needed it for my Next Record button.

What I did was to get the current record number that was in the record number entry box and save that into the variable x. Because it is a string I converted the string to an int y. Then used recNo = y + 1 and it seems to work ok. I just repeated the same thing but changed the recNo = y + 1 to recNo = y - 1 for the Previous Button.

def fetchRecord():
    x=recBox.get()
    y=int(x)
    recNo = y + 1

    ....
Thanks again for you help. Very much appreciated.
Reply
#6
I don't think that works. That's creating a local variable named recNo with the value y + 1, but that doesn't update the global variable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I didn't quite understand your example in that I saw that you incremented recNo and then returned it. At first look I was under the impression that it was returned right after it was incremented and that the rest of the function was not being executed. Having looked at it again, I think I now see what is actually happening. The recNo is incremented and after executing the rest of the function using the new value of recNo, then it is reutrned to update the global. I went back and changed my code to yours and it is working fine.

Thank you. I really do appreciate you help and your patience.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't list require global keyword? johnywhy 9 784 Jan-15-2024, 11:47 PM
Last Post: sgrey
  It's saying my global variable is a local variable Radical 5 1,149 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,111 May-12-2023, 04:16 PM
Last Post: deanhystad
  Why Pip is not listed in the official Global Python Module index? quazirfan 2 754 Apr-21-2023, 10:55 AM
Last Post: snippsat
  Delete all Excel named ranges (local and global scope) pfdjhfuys 2 1,768 Mar-24-2023, 01:32 PM
Last Post: pfdjhfuys
  Serial Port As Global Prasanjith 2 1,478 Mar-23-2023, 08:54 PM
Last Post: deanhystad
  global varname Skaperen 6 1,086 Mar-03-2023, 12:10 AM
Last Post: Skaperen
  Global variables or local accessible caslor 4 1,014 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,139 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to use global value or local value sabuzaki 4 1,147 Jan-11-2023, 11:59 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020