You've got one line of code backwards. Your if statement is effectively saying that if item is greater than Max, set item to Max. You need to flip that to say if item is greater than Max, set Max to item.
if item > Max:
Max = item
return Max
Also, I'm not an expert in Python, but i think you need to change the List inside your function to match the parameter name, in this case args.
Answer from Kadima on Stack OverflowIs max() an in-built function in Python?
What is the use of the max() and min() functions in Python?
What is the difference between the Python max() and min() functions?
You've got one line of code backwards. Your if statement is effectively saying that if item is greater than Max, set item to Max. You need to flip that to say if item is greater than Max, set Max to item.
if item > Max:
Max = item
return Max
Also, I'm not an expert in Python, but i think you need to change the List inside your function to match the parameter name, in this case args.
*args = list of arguments -as positional arguments
You are passing a list as an argument here. So your code should look something like this -
def maximum(nums):
Max = 0
for item in nums:
if item > Max:
Max=item
return Max
List = [1,5,8,77,24,95]
print maximum(List)
This would give you the result : 95.
On the other hand you can use the max built in function to get the maximum number in the list.
print max(List)
The answer of this code is wrong. The output is supposed to be 25 but for some reason it's 9.
Code:
list=['9', '12', '8', '25', '23']
biggest_number=max(list)
print(biggest_number)
I tried the following code with the following input in different IDE's but every single one of them gave the same answer, maybe the max module is taking the 9 as 90. Any comments or help would be appreciated to solve this issue.
In Python 3.4+, you can use default keyword argument:
>>> max([], default=99)
99
In lower version, you can use or:
>>> max([] or [99])
99
NOTE: The second approach does not work for all iterables. especially for iterator that yield nothing but considered truth value.
>>> max(iter([]) or 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: max() arg is an empty sequence
In versions of Python older than 3.4 you can use itertools.chain() to add another value to the possibly empty sequence. This will handle any empty iterable but note that it is not precisely the same as supplying the default argument as the extra value is always included:
>>> from itertools import chain
>>> max(chain([42], []))
42
But in Python 3.4, the default is ignored if the sequence isn't empty:
>>> max([3], default=42)
3