Nevermind, i erased the return statment over there and put it after the ‘for’ statment
I don’t think they really taught it but this is super simple to solve just using the max() method and it works as a solution.
Ok, question on potential solutions. I wrote the following solution. It yielded the correct result and to my eye is valid, yet the lesson wouldn’t let me progress. Is there a reason why I’m wrong?
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
def max_num(nums): final_value = 0 for num in nums: if num > final_value: final_value = num return final_value print(max_num([50, -10, 0, 75, 20]))
The feedback tells me the answer should have returned -20, but my solution returned 0.
In reality, my solution returned 75 - the same answer as the solution code.
Most of the lessons have background tests which aren’t visible to the user and I expect this warning to come from those checks (not from your
Try running your function with the following and consider if your output is still as expected:
print(max_num([-50, -10, -20]))
this is what i did
maximum = nums[-1]
for num in nums:
if num > maximum: num = maximum
I am getting 75 as the output sill its wrong.
def max_num(nums): max = 0 for i in nums: if i > max: max = i return max print(max_num([50, -10, 0, 75, 20]))
This will exclude negative numbers. Use a something from the inputs as a starting value.
Why did you use append here. largest_num is not a list then why used it here?
That’s precisely why it doesn’t work and they got the AttributeError. Unless we wanted to append the final max value to a list,
.append() would not be useful anyways to find the max value since we want to keep track of the largest number we’ve encountered so far.
i kinda went a different way with this task,
i found that i didnt need to use any loops… Even thou the task probably wanted me to.
def max_num(lst): lst.sort() return lst[-1]
They accepted my answer so i guess its a win ?
Be careful with a solution like this, if you now went and checked the list that you passed as an argument you’d find that you’ve altered the contents of that list (it is now ordered). This could have unintended side effects in a larger script.
ah yes ofcouse =)
Thanks for the advice.
Maybe ill try use a for loop
Can someone explain to me why for i in nums wont work? I somehow keep making this mistake. It gives: Indexerror: list index out of range. The error gave me the solution to use the range() function but its not connecting to me.
def max_num(nums): max = 0 for i in range(len(nums)): if nums[i] > max: max = nums[i] return max print(max_num([50, -10, 0, 75, 20]))
Because you’re setting
i to the contents of your list. Consider what happens when you then try and index a list using
i in this case. If you’re unsure how
for i in nums behaves try playing around with the code below.
nums = [10, 20, 30] for idx in list: print(idx)
Perhpas you could work at a solution without indexing?
Are there multiple ways to achieve the same result with this exercise or is there something wrong with my code?
My code was different than the expected result but still collected the correct answer.
This was my code:
I think it’s covered in a few other responses but consider what would happen with a list like- [-4, -39, -20]. By viewing it we can see the maximum but what would your function return? The safest option is generally to start with a value form the list itself.
Oh I see, if the list was all negatives then 0 would end up being the highest… That makes sense, thank you!
Is it necessary to create the var variable in your solution? I’ve seen so many people do it but I didnt do it in mine (it still works without it tbf).
I have an eerie feeling that i might be missing something or that my solution is flawed. Deffo lmk what I can do to improve or if the var variable is redundant.
Yes there is indeed a benefit to
var here so it’s a worthwhile question. You might’ve caught it in an earlier reply but with this you’re acutally modifying the underlying list with your current function. Here’s a quick example of how that might play out-
student_scores = [71, 63, 11, 88, 81, 65, 72, 69] print(max_num(student_scores)) # use the function you had Out: 88 print(student_scores) Out: [88, 63, 11, 88, 81, 65, 72, 69]
Uh-oh we’ve changed our student_scores. Lucky for the 1st student eh? In some circumstances this could lead to some very unintended behaviour so in this case I’d say
var is useful.
Thank you so much!! Clear as day