I want to return a new array from another array by rounding the value near to 5 and it shouldn't round if the rounded number is less then 40. but it is showing "IndexError: list assignment index out of range" error .
import os
import sys
#
# Complete the gradingStudents function below.
#
def gradingStudents(grades):
def round_to_next5(n):
return n + (5 - n) % 5
j = len(grades)
r = [j]
for i in range(0,len(grades)):
roundi = round_to_next5(grades[i])
dif = roundi - grades[i]
if dif < 3 and roundi > 40:
r[i] = roundi
print("working1")
else:
r[i] = grades[i]
print("working2")
return r
if __name__ == '__main__':
f = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
grades = []
for _ in range(n):
grades_item = int(input())
grades.append(grades_item)
result = gradingStudents(grades)
f.write('\n'.join(map(str, result)))
f.write('\n')
f.close()
Expected an array but it's showing an error.