unboundLocalError变量是字典中的键

2024-05-12 19:50:26 发布

您现在位置:Python中文网/ 问答频道 /正文

def create_school(school,year):
    all_students = {}
    achievements= {}
    var = data.Files.get(school,year)
    for child in var.student_info_parsed:
        for gchild in var.student_info_parsed[child]:
            for ggchild in var.student_info_parsed[child][gchild]:
                all_students[ggchild[0]] = ggchild
    for child in var.student_info:
        for gchild in var.student_info[child]:
            if gchild == 'localidentifier':
                student = var.student_info[child][gchild]
                achievements[student] = {}
                achievements[student]['certificates'] = []
                achievements[student]['otherachievements'] = []
            if gchild == 'certificates':
                for ggchild in var.student_info[child][gchild]:
                    cert = var.student_info[child][gchild][ggchild]
                    cert_year = cert[2][0:4]
                    if cert_year == year:
                        achievements[student]['certificates'].append(cert)
            if gchild == 'otherachievements':
                ocert = var.student_info[child][gchild]
                n = 0
                while n < len(ocert):
                    ocert_year = ocert[n][1]
                    if ocert_year == year:
                        achievements[student]['otherachievements'].append(ocert[n])
                    n = n+1
            if gchild == 'endorsements':
                ecert = var.student_info[child][gchild]
                l = list()
                for subj in ecert:
                    if ecert[subj]['courseendorsementresult'] != 'NO':
                        result = ecert[subj]['courseendorsementresult']
                        l.append([subj,result]) 
                achievements[student]['endorsements'] = l
    return achievements

所以在我添加条件之前

if gchild == 'endorsements':
    ecert = var.student_info[child][gchild]
    l = list()
    for subj in ecert:
        if ecert[subj]['courseendorsementresult'] != 'NO':
        result = ecert[subj]['courseendorsementresult']
            l.append([subj,result]) 
    achievements[student]['endorsements'] = l

所有的东西都能找到,但现在我错了

  File "/Users/Teacher/Documents/Python/Standards/Data_Manipulation_3/data_man.py", line 454, in create_school
    achievements[student]['endorsements'] = l
UnboundLocalError: local variable 'student' referenced before assignment

奇怪的是,到目前为止,我一直在引用“学生”这个词。你知道吗

我没有名为student的全局变量?你知道吗

我真的很困惑,我以为这些错误发生在全局变量和局部变量混淆的时候,但是“student”变量只是局部变量?你知道吗

任何帮助都将不胜感激。你知道吗


Tags: ininfochildforcertifvaryear
3条回答

变量student是在前面有if gchild == 'localidentifier'的块中创建的。如果执行该块,变量student将可用。但是,如果前面有if gchild == 'endorsements'的块在另一个块之前执行(因为gchild=='endorsements'在比gchild=='localidentifier'更早的迭代中),那么变量student将不会被创建。你知道吗

正如您在以下条件中定义的student

if gchild == 'localidentifier':
      student = var.student_info[child][gchild]

所以在你的第二个语句if gchild == 'endorsements':中,你没有定义student,因为你的两个条件都检查gchild的值,如果一个发生了,另一个没有!所以你有两个选择

  1. 在新语句中也定义学生。你知道吗
  2. 如果可能的话,在所有条件的顶部定义它。你知道吗

我猜gchild == 'endorsements'gchild == 'localidentifier'是互斥的—所以不会出现两个代码块都将运行的情况。您只在if gchild == 'localidentifier':块中为student赋值,因此,如果第一个gchild值是'endorsements',则student将不会被填充,最终会出现UnboundLocalError。你知道吗

我不知道您的代码的细节,但我猜您可能希望用类似于第一个if块的值填充studentvar,如下所示:

student = var.student_info[child][gchild]

或者可以使用ecert变量,该变量的值似乎与student的值相同:

if gchild == 'endorsements':
  ecert = var.student_info[child][gchild]
  l = list()
  for subj in ecert:
    if ecert[subj]['courseendorsementresult'] != 'NO':
    result = ecert[subj]['courseendorsementresult']
        l.append([subj,result]) 
  achievements[ecert]['endorsements'] = l

相关问题 更多 >