Reputation: 41
Getting the following error with my code ...
TypeError: '>=' not supported between instances of 'dict' and 'int'
Any idea why? Trying to run a loop where it looks at each student's weighted average score, and then assigns them a letter grade A - F ... For some reason the loop refuses to work when it tries to pull the letter grade.
Using python 3.7.6
steve = {"Name": "Steve",
"Homework": [90, 97, 75, 92],
"Quizzes": [88, 40, 94],
"Tests": [75, 90]}
alice = {"Name": "Alice",
"Homework": [100, 92, 98, 100],
"Quizzes": [88, 40, 94],
"Tests": [75, 90]}
tyler = {"Name": "Tyler",
"Homework": [0, 87, 75, 22],
"Quizzes": [0, 75, 78],
"Tests": [100, 100]}
print(steve)
print(alice)
print(tyler)
students = []
students = [steve, alice, tyler]
for i in students:
print(f"Name: {i['Name']}\nHomework: {i['Homework']}\nQuizzes: {i['Quizzes']}\nTests: {i['Tests']}")
numbers = []
def average(numbers):
return sum(numbers) / len(numbers)
def get_weighted_average(student):
homework_average = average(student["Homework"])
quiz_average = average(student["Quizzes"])
test_average = average(student["Tests"])
weighted_score = homework_average*.1 + quiz_average*.3 + test_average*.6
return weighted_score
print(f"Steve's: {get_weighted_average(steve)}")
print(f"Tyler's: {get_weighted_average(tyler)}")
print(f"Alice's: {get_weighted_average(alice)}")
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'
print(get_letter_grade(50))
print(get_letter_grade(100))
print(get_letter_grade(72.5))
for i in students:
print(f"Name: {i['Name']}'s weighted score is {get_weighted_average(i)}")
print(f"Name: {i['Name']}'s letter grade is: {get_letter_grade(i)}")
Upvotes: 3
Views: 12835
Reputation: 1260
This error is caused by the last two lines, where i
is a dictionary, and was used as an argument for the get_letter_grade
function, which should accept integers as an argument.
Did you mean get_letter_grade(get_weighted_average(i))
?
In the future, it would be good practice to check the data type of arguments in functinos so it would be easier for you to see where the error occurs.
Upvotes: 0
Reputation: 6053
At the end instead of get_letter_grade(i)
you need get_letter_grade(get_weighted_average(i))
Upvotes: 3