TypeError:'list'和'int'实例之间不支持'> ='吗?

时间:2019-12-07 19:32:33

标签: python

我不明白为什么我得到了">="的错误,我认为我可能打错了事。

TypeError: '>=' not supported between instances of 'list' and 'int'?
def determineLetterGrade(letterGd, testAverage):

    if (testAverage >= 90) and (testAverage <= 100):
        letterGd = "A.";
    else:
        if (testAverage >= 87) and (testAverage <= 89.99):
            letterGd = "B+.";
        else:
            if  (testAverage >= 80 and (testAverage <= 86.99)):
                letterGd = "B."
            else:
                if  (testAverage >= 77) and (testAverage <= 79.99):
                    letterGd = "C+. ";
                else:
                    if  (testAverage >= 70) and (testAverage <= 76.99):
                        letterGd = "C.";
                    else:
                        if  (testAverage >= 67) and (testAverage <= 69.99):
                            letterGd = "D+.";
                        else:      
                            if (testAverage >= 60) and (testAverage <= 66.99) :                  
                                letterGd = "D. ";
                            else:                       
                                letterGd = "F.";  
    return letterGd; 

def writeSortedList(count, finalCount, name, testAverage, outFile):
    count = 0;
    outFile.write("SORTED TEST AVERAGE LIST (LOWEST TO HIGHEST)" + "\n"); #writes the Label/title
    outFile.write("=" * 110 + "\n");
    while (count < finalCount):
        outFile.write(format(format(name[count], "30s") + format(" : ", "^5s") + format(testAverage[count], "6.2f") +  format(" -> ", "^5s") +  + "\n"));
        count = count + 1;
        #end while loop
    outFile.write("=" * 110 + "\n");
    outFile.write("\n");
    # End writeSortedList function

2 个答案:

答案 0 :(得分:1)

  

问题:我不明白为什么我遇到了">="

错误

您正在尝试将list类型的值与int类型的值进行比较。

  

例如:testAverage的类型为list,而90的类型为int

testAverage >= 90

请考虑以下内容:

def get_grade(average):
    if not isinstance(average, float):
        raise ValueError("Argument 'average' have to be of type 'float', got {}"
                  .format(type(average)))

    for r0, r1, g in ((90.0, 100.0, "A."), (87.0, 89.99, "B+."), 
                      (80.0, 86.99, "B."), (77.0, 79.99, "C+."), 
                      (70.0, 76.99, "C."), (67.0, 69.99, "D+."), 
                      (60.0, 66.99, "D.")):
        if average >= r0 and average <= r1:
            return g
    else:                       
        return "F."

print(get_grade([67.0]))
  

这将引发:
  ValueError: Argument 'average' have to be of type 'float', got <class 'list'>

但是这样做: print(get_grade(67.0))将输出:D+.

答案 1 :(得分:0)

我已经以Python可读格式修改了您的defineLetterGrade()函数:

def determine_letter_grade(test_average):
    if 90 <= test_average <= 100:
        letter_gd = "A."
    elif 87 <= test_average <= 89.99:
        letter_gd = "B+."
    elif 80 <= test_average <= 86.99:
        letter_gd = "B."
    elif 77 <= test_average <= 79.99:
        letter_gd = "C+. "
    elif 70 <= test_average <= 76.99:
        letter_gd = "C."
    elif 76 <= test_average <= 69.99:
        letter_gd = "D+."
    elif 60 <= test_average <= 66.99:
        letter_gd = "D. "
    else:
        letter_gd = "F."
    print(letter_gd)

我也建议您对函数/类/变量使用有意义的名称。建议使用letter_gd代替letter_grade

因此,当您使用参数调用此函数时,会得到结果。 determine_letter_grade(90)为您带来结果A+.