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()
7
  • 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. Commented Jun 11, 2018 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 Commented Jun 11, 2018 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. Commented Jun 11, 2018 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 Commented Jun 11, 2018 at 17:40
  • @user3483203 I understand! However, in this context n just so happens to be the length of the array grades! Commented Jun 11, 2018 at 17:41

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

9 Comments

@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.
@JoshDetwiler I disagree with the confusion. Return is a function and so it makes sense for many to put grades in brackets here.
@JoshDetwiler I see! I wont write it next time
Thanks all for helping!
@IcyBloom My pleasure. Also consider stackoverflow.com/questions/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: stackoverflow.com/q/20639180/8033585
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.