Loops assignment (max_num)

The assignment seems simple enough:

"Create a function named max_num() that takes a list of numbers named nums as a parameter.

The function should return the largest number in nums"

But something in the code I’ve written keeps returning 50 which is the first item in the list. It makes me think that something I’m doing creates a loop that doesn’t actually loop. It’s only looking at the first item in the list and then stopping.
Can someone please explain to me, in really simple terms (I’m new to coding and get lost in the lingo), why this code isn’t working?

I really appreciate the help. I keep having similar problems with my loops and haven’t been able to figure out what I’m doing wrong despite checking other forum post and just simply having the answer shown to me.
Please, I really need this explained like you are talking to someone with absolutely no coding experience, because that’s exactly who you’re dealing with.

Hey there,

def max_num(nums):
  largest = 0
  for x in nums:
    if x > largest:
      largest = x
      return largest  

So your code reads:

- set `largest` to 0
- for every element in the nums list:
  1. if that element is larger than `largest`, 
     set `largest` to that element
  2. otherwise (if the element is equal or smaller than `largest`), 
     return `largest` 
     (this will break the loop and the function at this point)

So that’s why any 2nd number that’s not bigger than 50 is going to end with the function returning 50.

Your list is [50, -10, 0, 75, 20], if the list were [20, -10, 0, 75, 50] it would return 20.

To be clear, the for-loop normally goes through every element unless you tell it to break (hence, why it’s stopping after the second element). This can be done with a break statement but a return statement is even more powerful and it will end the function altogether.

1 Like

@toastedpitabread to the rescue again.
So my problem is that I’ve told the code to stop the second it hits a number smaller than ‘largest’ has been assigned?
I know at some point I need to return ‘largest’, but how do I get it to run the entire list before doing that?
Could I change the else statement to:
if x < largest:
return largest

Would that work?
I really appreciate your help. Loops are killing me. I may end up having to find a tutor to help me through this with some in person interaction.

Your code was almost there

You don’t really need this if you think about it, so that’ll stop it from ending prematurely:

   return largest 

#you can cut the else and leave it at the end of the function
#outside of the for-loop and any conditionals

The only big question is: is it safe to set largest arbitrarily to 0, what if the list is all negative numbers?

There’s a couple of techniques for sorting the largest, but you can for example, just set the default of largest to be the first item of the list before you go into the loop. Another would be to sort the list and return the last element (no loop needed).

I think it’s just a matter of practice and parsing your logic into English/math so you can verify it works on paper.

But glad to be of any help!

1 Like

@toastedpitabread by saying I can cut it and leave it at the end of the function, would that be done by un-indenting that line to bring it to the same level of indention as the for statement?

I see what you mean about setting largest to 0…
When you say I could set it to the first item on the list, do you mean that I would set it like:
largest = nums.index()


By cutting I mean this

def max_num(nums):
  largest = 0 # note that this still would need to be edited
  for x in nums:
      if x > largest:
          largest = x
  return largest 

As for accessing items on a list (I would review this, slicing, and range techniques before getting too far into loops, it will save you a lot of headaches:

some_list = ["baguettes", "are", "strong", "rivals", "to", "pita"]

# rivals

# pita
1 Like

Ok, @toastedpitabread, final question… well… for this thread at least :upside_down_face:
Do you tutor or know anyone who does that would deal well with someone just starting out with coding?

That’s not something we can do in this forum :slight_smile:

You should look around, I’m sure there are tutors offering services (but you can’t ask here).

1 Like

Oh, sorry @toastedpitabread ,I didn’t realize that was an off limits subject on the forums. I’m not trying to get myself or anyone else in trouble. Sorry again.

1 Like

No worries! :slight_smile:

1 Like