简体   繁体   中英

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

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)"??

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)

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:

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.

In get_student_average() you are declaring results as 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 TypeError is coming from. You have to be sure to send get_letter_grade() an int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM