Bottom Page

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.
Quote
#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

Quote
#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.
Quote
#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

Quote
#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.
Quote
#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

Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Global Variables - Some Points Needing Clarification. adt 4 174 Nov-30-2019, 01:23 PM
Last Post: adt
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 341 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  use of global wpo 21 516 Oct-17-2019, 06:13 AM
Last Post: buran
  Declaring a Global Variable in a Function Bob1948 4 216 Sep-14-2019, 11:16 PM
Last Post: ichabod801
  Decimal (Global precision setting) Valentina 3 283 Aug-23-2019, 11:53 AM
Last Post: Valentina
  modifying variables in local or global space Skaperen 2 169 Aug-14-2019, 07:13 AM
Last Post: Skaperen
  passing an argument to avoid a global Skaperen 9 474 Jul-12-2019, 11:07 PM
Last Post: Skaperen
  Global variables not changing ThemePark 3 419 Mar-02-2019, 11:24 AM
Last Post: Larz60+
  help with threading module and passing global variables ricardons 1 786 Feb-21-2019, 12:48 PM
Last Post: stullis
  Use of global variables from several modules. Jstechg 3 488 Jan-03-2019, 03:39 AM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)