Search code examples
pythonpython-3.xpycharm

Why is 'NoneType' object not iterable in HackerRank, but not PyCharm?


Using Python 3.x, I'm trying to round up students grades given a teacher's weird way of grading. Basically, if the student grade is below 38, do nothing. If the difference between the grade and the next multiple of 5 is less than 3, round the grade up to the next multiple of 5. Otherwise, don't change the grade. Here is the code I've used in both PyCharm and HackerRank's web-based IDE:

grades = [37, 39, 52, 83, 91]

def gradingStudents(grades):
    for i in range(len(grades)):
        grade = grades[i]
        if grade > 38:
            for j in range(4):
               next_val = grade + j
               if ((next_val-grade) < 3) and (next_val%5 == 0):
                   grade = next_val
        print(grade)

gradingStudents(grades)

The output in PyCharm is correct:

37
40
52
85
91

For comparison, here is the code from from the web-based IDE in HackerRank (https://www.hackerrank.com/):

#!/bin/python3
import os
import sys

#
# Complete the gradingStudents function below.
#
def gradingStudents(grades):
    for i in range(len(grades)):
        grade = grades[i]
        if grade > 38:
            for j in range(4):
               next_val = grade + j
               if ((next_val-grade) < 3) and (next_val%5 == 0):
                   grade = next_val
        print(grade)

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))) #<-- This is line 32 from error!
    f.write('\n')

    f.close()

This throws the following error:

Traceback (most recent call last):
File "solution.py", line 32, in <module>
f.write('\n'.join(map(str, result)))
TypeError: 'NoneType' object is not iterable

Solution

  • Hackerrank is expecting a list of grades to be returned from your function, you are returning nothing, just printing.

    def gradingStudents(grades):
        result = []  # make an empty list
        for i in range(len(grades)):  # assuming this logic is correct
            grade = grades[i]
            if grade > 38:
                for j in range(4):
                    next_val = grade + j
                    if ((next_val-grade) < 3) and (next_val%5 == 0):
                        grade = next_val
    
            result.append(grade)  # append the grade to list
        return result # return the list, you had no return statement so it was returning none