Can you help me understand why my code doesn't work?

Hello all,

for some reason my code returns the value 50 but I am not sure why. In the end, I requested to see the solution and it turns out, it is the same as mine (except for the name of my variables)

#Write your function here
def max_num(nums):
maxNum = nums[0]

for i in nums:
if i > maxNum:
maxNum = i
return maxNum

#Uncomment the line below when your function is done
print(max_num([50, -10, 0, 75, 20]))

Could anyone please assist in identifying why the output is 50 (and of course marked wrong)?

We cannot tell from looking at the code but I suspect the return statement is inside the loop body.

1 Like

Thank you very much for your reply my. The code in the message didn’t display as it did when I pasted it in. I have another check over my return statement.

Andrea Hopkins


For future reference regarding how to post code:

Happy coding!

This really useful info thank you

Andrea Hopkins

1 Like