gpt4 book ai didi

python - 我 pickle 了我的元组

转载 作者:太空狗 更新时间:2023-10-29 18:34:18 25 4
gpt4 key购买 nike

我对 python 还是个新手。我正在为一个更大的项目设计框架。这个程序让你想到一个圆或一个正方形,然后它会问四个问题,然后决定一个答案。

我在框架的最后一步,但遇到了问题。我收到“未定义全局名称‘qas1’”

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

这发生在我尝试 pickle 我的元组时。

这是我的加载程序,用于创建包含我的元组的 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 时,它重新确认它是一个元组,但是当我尝试将属性传递给 getQuestion() 中的“问题”时:它忘记了什么他们是。任何理想问题是什么?

最佳答案

在你的第二个文件中,qas[1-4] 都是 hasFile 函数的局部变量。将它们设置为全局,您的代码将起作用:

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

同样的错误出现在第一个代码中,但更难注意到 - 四个变量仍然只在函数中赋值 - 这隐藏相同名称的全局变量,使它们保持不变。但是,由于预计加载它们不会更改其内容,因此它似乎有效。

关于python - 我 pickle 了我的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com