我正在解决一个简单的hackerrank问题来给学生评分。我用python编写了代码,但代码逻辑似乎不正确,因为预期的输出没有出现。在
https://www.hackerrank.com/challenges/grading/problem在这里,您可以找到问题陈述。在
问题是
HackerLand University has the following grading policy: Every student receives a grade in the inclusive range from 0 to 100 . Any less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student's grade according to these rules: If the difference between the grade and the next multiple of 5 is less than 3, round up to the next multiple of 5. If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade. For example, grade = 84 will be rounded to 85 but grade = 29 will not be rounded because the rounding would result in a number that is less than 40.
Given the initial value of grade for each of Sam's students, write code to automate the rounding process.
def gradingStudents(grades):
#
# Write your code here.
#
for x,i in enumerate(grades):
if(i>=38) and (i%5)>=3:
grades[x]=i+5-(i%5)
return (grades)
这是我写的代码。在
输入
^{pr2}$我的输出是
75, 67, 38, 33
但预期是38应该四舍五入到40
问题是一旦处理完第一个数字,
return
。 相反,请等到您处理完所有问题:输出:
^{pr2}$你的问题是由于缩进。在
请注意,return语句与if语句对齐,它们都在循环中。因此,您的代码实际上只检查第一个元素,然后返回等级,可能只更改第一个等级。在
一旦您取消了return语句的缩进,使其脱离for循环,您的代码就可以工作了。在
相关问题 更多 >
编程相关推荐