Python 类型错误:“list”和“int”的实例之间不支持“>=”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43403992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:01:19  来源:igfitidea点击:

TypeError: '>=' not supported between instances of 'list' and 'int'

pythonpython-3.x

提问by Max

Hi I'm stuck with the above error. The problem come out with the last function "get_student_average". If "results" store the "get_average(student)" value, why it can't give me back the result of "get_letter_grade(results)"??

嗨,我被上述错误困住了。问题出在最后一个函数“get_student_average”上。如果“results”存储“get_average(student)”值,为什么它不能给我返回“get_letter_grade(results)”的结果??

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 97.0, 98.0, 100.0],
    "quizzes": [98.0, 99.0, 99.0],
    "tests": [100.0, 100.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 35.0, 45.0, 22.0],
    "quizzes": [0.0, 60.0, 58.0],
    "tests": [65.0, 58.0]
}
students = [lloyd,alice,tyler]
def average(numbers):
    total= sum(numbers)
    total = float(total)
    return total / len(numbers) 

def get_average(student):
    homework= average(student["homework"])
    quizzes= average(student["quizzes"])
    tests= average(student["tests"])
    return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

def get_student_average(gruppo):
    for student in gruppo:
        results= []
        results.append(get_average(student))
        print (student["name"])
        print (results)
        print (get_letter_grade(results))

get_student_average(students)

回答by Robert Valencia

I think your intention for the last print statement is to print the letter grade of the student for each iteration. To do that, you should pass the result of the current student to the get_average function, not the current running list of results:

我认为您对最后一个打印语句的意图是为每次迭代打印学生的字母等级。为此,您应该将当前学生的结果传递给 get_average 函数,而不是当前正在运行的结果列表:

def get_student_average(gruppo):
    for student in gruppo:
        results= []
        result = get_average(student)
        results.append(result)
        print (student["name"])
        print (result)
        print (get_letter_grade(result))

get_student_average(students)

See if that works for you.

看看这是否适合你。

回答by Gardo

In get_student_average()you are declaring resultsas a list and then you are passing it to get_letter_grade(). get_letter_grade()is then comparing a list to a number which is where the TypeErroris coming from. You have to be sure to send get_letter_grade()an int.

get_student_average()你声明results为一个列表,然后你将它传递给get_letter_grade(). get_letter_grade()然后将列表与来自的数字进行比较TypeError。你必须确保发送get_letter_grade()一个int。