Write a function is_better_than_avg(students, pos)
, that takes two arguments students
is the list of all students' dictionaries position is an integer - position of the student in the list we want to check. Checking works like this - count the average of all grades in the school (upper cell) compare the value to the student's average. Return True
if the student has an avg greater than the school avg, otherwise, return False
.
I don't know why the second execution of the program says True
instead of False
.
# Function for testing purposes
def test(got, expected):
if got == expected:
prefix = ' OK '
else:
prefix = " Something's wrong "
print(prefix,' got:',repr(got),'expected:',repr(expected))
list_of_students = []
list_of_students.append(kosma) # appending the student k1sma
def is_passed(student_dict):
return avg(student_dict)>=3.0
def school_avg(students):
all = 0
licznik = 0
for a in students:
all = all avg(a) #dodaje średnią sprawdzanego studenta do 'all'
licznik = licznik 1 #dodaje 1 do liczniku
return all / licznik
list_of_students.append({
"username": "mikub",
"age":24,
"is_blocked": False,
"grades": [5,5,5,5]
})
list_of_students.append({
"username": "ewa",
"age":24,
"is_blocked": False,
"grades": [1,3,3,2,1,1,1,1,1,1]
})
list_of_students.append({
"username": "robert",
"age":24,
"is_blocked": False,
"grades": [5,5,5,5,5,1]
})
list_of_students.append({
"username": "radek",
"age":24,
"is_blocked": False,
"grades": [1,3,3,2,5,5,5]
})
list_of_students.append({
"username": "kasia",
"age":24,
"is_blocked": False,
"grades": [5,5,5,1]
})
list_of_students.append({
"username": "kasia",
"age":24,
"is_blocked": False,
"grades": [5,1,1,1,1,5,1]
})
list_of_students.append({
"username": "jola",
"age":24,
"is_blocked": False,
"grades": [5,4,5,5,5,4]
})
#this is the important part
def is_better_than_avg(students,pos):
for pos in students:
if avg(pos) > school_avg(students):
return True
elif avg(pos) < school_avg(students):
return False
test(is_better_than_avg(list_of_students,0),True) #returns True as it should
test(is_better_than_avg(list_of_students,6),False) #idk why this returns True as well
CodePudding user response:
I think you should change the is_better_than_avg
function like this :
def is_better_than_avg(students,pos):
student = students[pos]
if avg(student) > school_avg(students):
return True
elif avg(student) < school_avg(students):
return False