# FAQ: Code Challenge: Loops - Max Num

This community-built FAQ covers the “Max Num” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Why isn’t this valid?

11 posts were split to a new topic: Can I solve this problem using sort or sorted?

3 posts were split to a new topic: My code and how it works

2 posts were split to a new topic: Could I utilize max() to solve this?

Here’s my solution

``````def max_num(nums):
var = [nums[0]]

for num in nums:
if num > var[0]:
var[0] = num
return var[0]
``````
2 Likes

4 posts were split to a new topic: Could this be solve with list comprehension?

5 posts were split to a new topic: Can you help me understand why my code doesn’t work?

2 posts were merged into an existing topic: When finding the largest or smallest element of a list, do we always have to check every value?

I feel like my code is correct and seems to be returning the correct value, but the answer/feedback box seems to be referring to a different list to the question and not registering my value correctly, so it’s not letting me progress

I’ve used the following code:
‘’’

def max_num(nums):

max_val = 0

for i in nums:

``````if i > max_val:

max_val = i
``````

return max_val

#Uncomment the line below when your function is done

print(max_num([50, -10, 0, 75, 20]))
‘’’
As you can see in the image below it returns 75 (which i’m sure is correct), but the feedback box is using a different list and saying i’ve returned 0

Is this an error on the site, or am I missing something?

`max_value` should be initialized to a value from the input list; i.e., `max_val = nums[0]`

Can you explain why that is? It really doesn’t make any sense to me why that is necessary. The output is the same regardless. It also doesn’t explain where max_num([-50, -20]) is even coming from.

Maybe you can motivate why it wouldn’t be necessary.
For example, do you write something else to it before reading it, or, if you don’t, then what value would it have?

The argument that it works for one input is VERY much insufficient. There are many possible inputs. You’d want a more general argument than that.

The image of the code referred to initiates it with 0. Is 0 something that is always in the array and would therefore be a valid result? Because the maximum value is a value that is found in the array. Right?

Maybe I’m just not understanding you. If I set the value to 0 and not nums[0] and it loops through, checks all indices for a greater value and updates it when a value that meets the criteria is found. I don’t see how that isn’t sufficient, and I don’t see

There it is. I’m reading your post as it is being updated and it makes sense now. Just leaving what I initially started responding with because I think other people may have my thought process. It seems whoever created this module put in some type of check specifically for people like me.

Just to make sure I understand though, the reason initializing to zero is a bad idea, is because at the end of the function, it is going to return max_val whether or not it was ever in the list and that is obviously not okay when we are only wanting the response to have been in the list.

Thanks for the help!

2 Likes

Making assumptions about input is probably a pretty common bug. You should be very suspicious of any kind of information in code that seemingly comes from nowhere. That zero comes from nowhere, there’s nothing special about 0, there’s no inherent relationship to maximum, so this is the kind of thing where you might be able to spot a bug before you’ve understood the bug or the code.

1 Like

I solved this without using a loop as the instructions didn’t say I had to use one, and this seemed the shortest way to solve it. I’m curious how I could still use my method but do it in a loop. I can’t see how the two are compatible.

``````def max_num(nums):

nums.sort()
return nums[-1]

print(max_num([50, -10, 0, 75, 20]))
``````

Seems like a lot of extra work to sort all the other values as well when you’re only interested in which of them is largest

I did the same exact thing. shrug the code works as intended.

The error message is showing how it isn’t working as intended >.>

My reply was to Beau. There is no error code. The result is the correct answer and only a slight variation to the “solution” but both use the same logic and provide the answer regardless of input.