为什么“ NoneType”对象在HackerRank中不可迭代,而在PyCharm中不可迭代?

时间:2018-11-17 04:42:42

标签: python python-3.x pycharm

使用Python 3.x,鉴于老师的怪异评分方式,我正在尝试汇总学生的评分。基本上,如果学生成绩低于38,则什么也不做。如果等级与下一个5的倍数之间的差小于3,则将等级四舍五入到下一个5的倍数。否则,请勿更改等级。这是我在PyCharm和HackerRank的基于Web的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)

PyCharm中的输出正确:

37
40
52
85
91

为进行比较,下面是HackerRank(https://www.hackerrank.com/)中基于Web的IDE中的代码:

#!/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()

这将引发以下错误:

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

3 个答案:

答案 0 :(得分:1)

Hackerrank希望从您的函数中返回一个等级列表,您什么也没返回,只是打印而已。

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

答案 1 :(得分:1)

问题是因为您的函数不返回任何内容或返回None,因此join将返回错误。 print仅打印该值,不返回任何内容,是的,它在您的PyCharm IDE中打印,但没有任何返回值。

如果您要使用这些值,则在添加您的逻辑后,通过添加f_grades(在函数中为列表),我对其代码进行了少许修改,以返回新列表。

import os
import sys

#
# Complete the gradingStudents function below.
#
def gradingStudents(grades):
    f_grade = []
    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
                        f_grade.append(grade)
    return f_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))) #<-- No more error!
    f.write('\n')

    f.close()

它执行的是在不传递整个代码的情况下,在通过逻辑并返回该列表之后,将这些值添加到新列表中。从那里,您将可以将功能应用于该列表,例如join

答案 2 :(得分:1)

这里是一种列表理解方法:

def gradingStudents(grades):
    return [5 * (x // 5) + 5 if x > 38 and x % 5 > 2 else x for x in grades]

print(gradingStudents([37, 39, 52, 83, 91]))
# [37, 40, 52, 85, 91]

考虑到它的理解力和简洁性,这样做会更有效率。