+ 1
What solved this test ?
You are required to complete the function maximum(x). where "x" is a list of numbers, and contains more than 2 numbers. the function is expected to return the highest number in that list. Example: input : [5,20,12,6] output: 20 you can change the numbers in the list no_list but you are not allowed to change the variable names or edit any other code, except the function's body, doing so may jeopardize your evaluation These are instructions for solution In [ ]: no_list = [1,2,3,4]def maximum(no_list): #complete the function to return the highest number in the listprint(maximum(no_list))
3 odpowiedzi
+ 2
NINJA PEACE what if all the numbers are negative? Then your preset value of 0 will be the highest number and the returned value will be incorrect. Also, using 'max' as a variable name isn't a good idea as it will overwrite the built-in function.
def maximum(no_list):
m = no_list[0]
for num in no_list[1:]:
if num > m:
m = num
return m
Or, if allowed;
def maximum(no_list):
return max(no_list)
But this is really redundant, so probably not allowed.
+ 1
Negm Abozed
I just supplied the maximum function. You have to supply the rest. If you can't figure it out or there are other issues, just saying it doesn't work, doesn't help. Post your code and what you have tried along with a good description of what the issue(s) is/are. More info is better than a vague useless reply.
0
Negm Abozed Try this:
ln [ ]:
def maximum(no_list):
max = 0
for x in no_list:
if x > max:
max = x
print(max)
maximum(no_list)