NameError:未定义名称“标记”

时间:2015-02-25 00:30:39

标签: python nameerror

import random

def bubbleSort(data):
    count = 0
    for i in range(0,len(data) - 1):
        for j in range(0, len(data) - 1):
            count += 1
            if data[j] > data[j+1]:
                #swap
                temp = data[j]
                data[j] = data[j + 1]
                data[j + 1] = temp
    print(count)
    return data

data = [5,4,3,2,1]
data = bubbleSort(data)
print(data)



def getData():

    data = []

    for i in range(0, 100):
        data.append(random.randint(0,100))
    return data




def mean(data):

    total = 0

    for i in range (0, len(data)):
        #add data[i]
        total = total + data[i]

    return total/ len(data)


def maximum(data):

    maximum = data[0]

    for i in range(0, len(data)):
        if maximum < data[i]:
            maximum = data[i]

    return maximum



#def minimum(data):

#def mode(data):




def getFrequency(marks):
    freqList = []
    for i in range(0,101):
        freqList.append(0)

    for i in range(0, len(marks)):
        mark = marks[i]
        freqList[mark] += 1

    return freqList


freq = getFrequency(marks)
mode = maximum(freq)

#print (freq)







marks = getData()

mean = mean(marks)

print(mean)

到目前为止这是我的代码。我是编程新手,所以很多都是错的。我首先要知道的是我的标记&#39;未定义

2 个答案:

答案 0 :(得分:1)

您已在第marks = getData()行中注明了标记,但您在freq = getFrequency(marks)中的这一行之前引用了该标记。

答案 1 :(得分:0)

您正在将marks传递给getFrequency,但此时名为marks的变量不存在。

marks = getData()移到getFrequency(marks)

之上

另外,为了避免将来出现问题,请注意以下几行:

mean = mean(marks)

此时,您将mean(marks)的结果存储在名为mean的变量中,但现在您将无法将该函数调用为mean是指variable

将存储结果的变量重命名为以避免以后出现混淆/错误,例如

mean_result = mean(marks)