Question

I'm still new to python. I'm working the framework for a larger project. This program makes you think of either a circle or square, then it ask four questions, then decides on an answer.

I'm on the last step of the framework, but ran into a problem. I get "global name 'qas1' is not defined"

Line 50 in getQuestion question = 'qas' Global name 'qas' is not defined

This happend when I tried to pickle my tuples.

Here is my loading program to create the pickle file that contains my tuples:

import cPickle
import os

qas1 = [
('Are you more like Waffle or a Pancake'), 
('1. Waffle', 1, 0),
('2. Pancake', 0, 1)
]

qas2 = [
('Do you have a straight edge?'),
('1. Yes', 1, 0),
('2. No', 0, 1)
]

qas3 = [
('Are you simliar in shape to a lolipop?'),
('1. Yes', 0, 1),
('2. No', 1, 0)
]
qas4 = [
('Is the world rounded like a planet, or flat like a map?'),
('1. Rounded', 0, 1),
("2. Flat", 1, 0)
]

def hasFile():
    print 'I see the file'
    qas_file = open('qas.dat', 'r')
    qas1 = cPickle.load(qas_file)
    qas2 = cPickle.load(qas_file)
    qas3 = cPickle.load(qas_file)
    qas4 = cPickle.load(qas_file)
    qas_file.close
    confirmer()

def noFile():
    print 'I dont see a file...'
    saver()

def confirmer():
    print qas1
    print qas2
    print qas3
    print qas4

def saver():
    qas_file = open('qas.dat', 'w')
    print 'No worries, creating one now'
    cPickle.dump(qas1, qas_file)
    cPickle.dump(qas2, qas_file)
    cPickle.dump(qas3, qas_file)
    cPickle.dump(qas4, qas_file)
    qas_file.close
    print 'all done'

fname = "qas.dat"
if os.path.isfile(fname):
    hasFile()
else:
    noFile()

The code worked okay, but when I tried to use the file that it created I ran into problems.

import cPickle

#Counters
counterCircle = 0
counterSquare = 0

# tuples
def hasFile():
    print 'I see the file'
    qas_file = open('qas.dat', 'r')
    qas1 = cPickle.load(qas_file)
    qas2 = cPickle.load(qas_file)
    qas3 = cPickle.load(qas_file)
    qas4 = cPickle.load(qas_file)
    qas_file.close



#varibles Im made to assign
messageDisplayed = 0
question = 'beer'

#prints to screen   
def showQaA():
    print question[0]
    print question[1][0]
    print question[2][0]

#recieves and implements responses
def getResponce():
    global counterCircle
    global counterSquare
    global qas1, qas2, qas3, qas4
    ansew = raw_input('>> ')
    if ansew == "1":
        counterSquare = counterSquare + question[1][1]#(+1)
        counterCircle = counterCircle + question[1][2]#(+0)
    elif ansew == "2":
        counterSquare = counterSquare + question[2][1]#(+0)
        counterCircle = counterCircle + question[2][2]#(+1)
    print counterCircle
    print counterSquare


#Gets the current tuple infomation to display (Will be more advanced)
def getQuestion():
    global question
    if messageDisplayed == 0:
        question = qas1
    elif messageDisplayed == 1:
        question = qas2
    elif messageDisplayed == 2:
        question = qas3
    elif messageDisplayed == 3:
        question = qas4
    else:
        print 'youre screwd'

#figures out and prints results
def results():
    print "This is the circle results", counterCircle
    print "This is the square results", counterSquare
    if counterSquare < counterCircle:
        print "You are a circle!"
    elif counterSquare > counterCircle:
        print "You are a square!"
    else:
        print "You are the elusive squircle!"

#mainLoop currently set to 4 questions
hasFile()
while messageDisplayed <=3:
    getQuestion()
    showQaA()
    getResponce()
    messageDisplayed +=1
results()

It is a lot of code to look at, I know. When the program first loads the name qas1 it reconizes that it is a tuple, but when I try to pass the properties to 'question' in getQuestion(): it forgets what they were. Any ideals what the problem is?

Was it helpful?

Solution

In your second file, qas[1-4] are all local to the hasFile function. Make them global, and your code will work:

def hasFile():
   global qas1, qas2, qas3, qas4
   # etc

The same bug occurs in the first code, but it is harder to notice - the four variables are still assigned only in the function - this shadows the identically-named global variables, leaving them unchanged. However, since loading them isn't expected to change their content, it appears to work.

OTHER TIPS

In hasFile(), you are setting the values of qas1, qas2, qas3, and qas4, but you aren't declaring them as global first, which means they aren't set in the global scope.

# tuples
def hasFile():
    global qas1, qas2, qas3, qas4 # <-- global declaration
    print 'I see the file'
    qas_file = open('qas.dat', 'r')
    qas1 = cPickle.load(qas_file)
    qas2 = cPickle.load(qas_file)
    qas3 = cPickle.load(qas_file)
    qas4 = cPickle.load(qas_file)
    qas_file.close

In every function you set global variables, you need to explicitly declare them as global. See the answers to this question.

Also, in the future, consider passing values to functions instead of using global variables, which can be error-prone, especially for beginners.

When you just assign a variable like qas with a new value, as you do in hasFile(), you're creating a local var... not a global. Make sure to specify global qas before you simply assign to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top