我还是python的新手。我正在为更大的项目工作框架。这个程序让你想到一个圆圈或正方形,然后它会问四个问题,然后决定一个答案。
我在框架的最后一步,但遇到了问题。我得到了全球名称' qas1'没有定义"
getQuestion问题中的第50行=' qas'全球名称' qas'未定义
当我试图腌制我的元组时,就会发生这种情况。
这是我的加载程序,用于创建包含我的元组的pickle文件:
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()
代码工作正常,但是当我尝试使用它创建的文件时遇到了问题。
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()
我知道,要查看的代码是很多。当程序首次加载名称qas1
时,它会重新确认它是一个元组,但是当我尝试将属性传递给问题时,问题是&#39;在getQuestion()
中:它会忘记它们是什么。任何理想问题是什么?
答案 0 :(得分:4)
在第二个文件中,qas [1-4]都是hasFile
函数的本地文件。使它们成为全球性的,您的代码将起作用:
def hasFile():
global qas1, qas2, qas3, qas4
# etc
第一个代码中出现了同样的错误,但是很难注意到 - 四个变量仍然仅在函数中分配 - 这个阴影同名的全局变量,保持不变。但是,由于加载它们不会改变其内容,因此它似乎可行。
答案 1 :(得分:1)
在hasFile()中,您设置的是qas1,qas2,qas3和qas4的值,但您并未将它们声明为全局优先,这意味着它们未在全局范围内设置。
# 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
在您设置全局变量的每个函数中,您需要将它们显式声明为全局变量。请参阅the answers to this question。
此外,将来考虑将值传递给函数而不是使用全局变量,这可能容易出错,特别是对于初学者。
答案 2 :(得分:1)
如果您只是像hasFile()
那样使用新值分配qas这样的变量,那么您将创建一个本地变量...而不是全局变量。在分配给它之前,请务必指定global qas
。