We won’t be able to tell. Perhaps go back to the new users section and learn how to format code samples in posts. The link is everywhere yet some of us can never find it. I know it’s there, somewhere.

Why this code does not get the negative list right?
like
[-50, -20] # it gives me -50

#Write your function here

def max_num(nums):
larger = 0
for i in range(len(nums)-1):
if nums[i] > nums[i + 1]:
larger = nums[i]
return larger
#Uncomment the line below when your function is done
print(max_num([50, -10, 0, 75, 20]))

Hey Josh!
I had the same issue and dealt with it this way. I created a new variable and then, by using it, I found THE SMALLEST value in the list. After, using the same variable (which is now equals to the smallest value in list) I found the largest one. Maybe it’s not the most efficient way to solve this exercise, but it works.

def max_num(nums):
maximum = 0
for i in range(len(nums)):
if nums[i] <= maximum:
maximum = nums[i]
for i in range(len(nums)):
if nums[i] >= maximum:
maximum = nums[i]
return maximum

This is because You’re setting up var maximum as = 0 and negative numbers are always smaller than 0 so it would always return 0 when you’re comparing it with negative numbers

Welcome to the forums, @digital2728968217. Not sure why the return is inside the outer loop. Can you explain your logic?

>>> max_num([320, 189, 67, 54, 98, 42, 256])
Traceback (most recent call last):
File "<pyshell#151>", line 1, in <module>
max_num([320, 189, 67, 54, 98, 42, 256])
File "<pyshell#148>", line 4, in max_num
if nums[i] < nums[k+1]:
IndexError: list index out of range
>>>

I used return inside for loop to stop first “for loop” from picking next element. But that code was not giving required result so i updated my code. please check it out and suggest me if any correction require:

def max_num(nums):
greatest = nums[0]
for i in range(len(nums)):
if nums[i] > greatest:
greatest = nums[i]
return greatest

i think this is an another way to get greatest number but it is not that efficient.

def max_num(nums):
for i in range(len(nums)):
for k in range(len(nums)):
if nums[i] >= nums[k]:
if k == len(nums)-1:
greatest = nums[i]
return greatest

Since the previous post contains a working version, what is gained by adding to the complexity? Also, a return nested in the inner function is never a good sign.

In the previous post my code was checking for “if nums[i] < nums[k]”. If no number is less than nums[i] i could not able to return the value of nums[i] anywhere. if i put return outside of the loop then it will pick another nums[i].
At the end that code had too many drawbacks so i dropped it and rewrite this code to implement the same logic but with different approach.