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?