我是一名大学生,总结了小组工作的代码,我朋友的代码可以正常运行,但是当我将其复制到整个文件中并且什么都没更改时,它只会返回:NameError:全局名称“ questions”不是定义
使用的原始代码:从Tkinter导入Tk,Frame,Label,Button。当我使用时:导入Tkinter。但是在总结时,我在代码的顶部都导入了该代码,但问题仍未解决,因此我添加了Tkinter。在每个地方都可能需要它之前,它仍然无法正常工作。
我现在感到非常绝望,如果您能帮助我,那将非常有帮助!!谢谢!
from Tkinter import Tk, Frame, Label, Button
import Tkinter
import math
import tkMessageBox
import re
from time import sleep
def askQuestion():
global questions, window, index, button, right, number_of_questions
if(len(questions) == index + 1):
Tkinter.Label(window, text="Thank you for taking the quiz. " + str(right) + " of " + str(number_of_questions) + " questions were answered correctly!").pack()
return
button.pack_forget()
index += 1
questions[index].getView(window).pack()
questions = []
file = open("questions.txt", "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
file.close()
我正在尝试创建一个空字典,并让它读取我准备的测验文件,我希望'questions'可以读取该文件,但它显示:NameError:全局名称'questions'未定义< / p>
答案 0 :(得分:-1)
您需要在使用全局变量的方法之前对其进行定义。