2

so I was doing a practice question on another website when I chanced upon this problem. The premise is that I need to round an array of numbers to the nearest multiple of 5.

If the number is less than 38, no rounding will occur. If it is more than 38, check if difference between grades[x] and nearest multiple of 5 is less than 5. If true: rounded up, else no rounding

My problem here is: if I input 4, 73, 67, 38, 33, 38 will not be rounded even though it is supposed to be rounded. However, when I remove the line return(grades) it will be rounded correctly. I can't seem to understand why. Can anyone help?

def gradingStudents(grades):
    for x in range(n):
        if grades[x] >= 38:
            if grades[x] % 5 >= 3:
                grades[x] = grades[x]+5-grades[x]%5
        return(grades)

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, grades)))
f.write('\n')

f.close()
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
IcyBloom
  • 333
  • 2
  • 11
  • 1
    If you store the result of the function as `result`, why are you still joining `grades`, and not result? That makes the return redundant anyways. – user3483203 Jun 11 '18 at 17:37
  • Ah yes that is true! But don't mind that! That was simply the code given as a template by the website that I am learning from! I believe that the original template is f.write('\n'.join(map(str,result))) instead, as it was contextualised to like running a script targetting a textfile of grades. However, I was unable to make 'result' a proper iterable – IcyBloom Jun 11 '18 at 17:38
  • 1
    You return statement is indented incorrectly. It's inside the loop, so it's causing the loop to prematurely exit. Move it to the left so that it's outside the loop. – Tom Karzes Jun 11 '18 at 17:40
  • Also, you really shouldn't use `for x in range(n)`, you should instead use `for x in range(len(grades))` or something like that – user3483203 Jun 11 '18 at 17:40
  • @user3483203 I understand! However, in this context n just so happens to be the length of the array grades! – IcyBloom Jun 11 '18 at 17:41
  • @TomKarzes I tried that too, but the loop runs correctly but still gives me the unrounded number 38 instead of 40 @.@ – IcyBloom Jun 11 '18 at 17:41
  • @IcyBloom The answer you accepted is doing exactly what I told you to do. Perhaps you didn't understand my comment? – Tom Karzes Jun 11 '18 at 18:00

1 Answers1

1

You have return(grades) inside the loop. Therefore the loop does not complete and exits the function too soon. Move return out of the loop:

def gradingStudents(grades):
    for x in range(n):
        if grades[x] >= 38:
            if grades[x] % 5 >= 3:
                grades[x] = grades[x]+5-grades[x]%5
    return(grades)

If you do not want to modify input argument, then do something like this:

def gradingStudents(grades):
    return [x+5-x%5 for x in grades if x >= 38 and x%5 >= 3]
AGN Gazer
  • 8,025
  • 2
  • 27
  • 45
  • @JoshDetwiler I do not see how one could get confused into thinking this is a tuple. I do not think this is an issue of any significance. – AGN Gazer Jun 11 '18 at 17:47
  • @JoshDetwiler I disagree with the confusion. Return is a function and so it makes sense for many to put grades in brackets here. – Bayko Jun 11 '18 at 17:47
  • @JoshDetwiler I see! I wont write it next time – IcyBloom Jun 11 '18 at 17:47
  • @Bayko: So if `return` was a function, what is its return value? And how would one specify a return value, then. Also try `print = 1` in Python, overwrites the print function just fine. With `return = 1` you get a syntax error, just as `if = 1`. So it is syntax, not just a function. – Martin Ueding Jun 11 '18 at 17:48
  • [`return` is not a function.](https://stackoverflow.com/a/4978586/7851115) –  Jun 11 '18 at 17:50
  • @Bayko No, return is not a function, it's a language statement. Very different. – Tom Karzes Jun 11 '18 at 17:50
  • I agree `return` is a statement. However I will not get into PEP8 discussions or what one likes and what one does not like. Indeed, pesonally I would not have used parenthesis but I am not as critical to their use as some are. – AGN Gazer Jun 11 '18 at 17:51
  • 1
    Thanks all for helping! – IcyBloom Jun 11 '18 at 17:53
  • 1
    @IcyBloom My pleasure. Also consider https://stackoverflow.com/questions/50803272/getting-a-different-result-when-using-and-not-using-return-in-python-3/50803322#comment88612382_50803272 comment. _Usually_ when a function modifies input argument, it does not return it as well (unless you document this behavior). Also, that nested `if` can be combined into a single `if` using logical `and`. Finally, consider using list comprehension: https://stackoverflow.com/q/20639180/8033585 – AGN Gazer Jun 11 '18 at 17:58