Question: I'm still receiving an error in my python code The call clamp([ -1 , 1 , 3 , 5 ], 0 , 4 ) returns

I'm still receiving an error in my python code "The call clamp([-1, 1, 3, 5], 0, 4) returns None, not [0, 1, 3, 4]." 

def clamp(alist,min,max):

"""

Returns a copy of alist where every element is between min and max.

Any number in the list less than min is replaced with min.Any number

in the tuple greater than max is replaced with max. Any number between

min and max is left unchanged.

Examples:

clamp([-1, 1, 3, 5],0,4) returns [0,1,3,4]

clamp([-1, 1, 3, 5],-2,8) returns [-1,1,3,-5]

clamp([-1, 1, 3, 5],-2,-1) returns [-1,-1,-1,-1]

clamp([],0,4) returns []

Parameter alist: the list to copy

Precondition: alist is a list of numbers (float or int)

Parameter min: the minimum value for the list

Precondition: min <= max is a number

Parameter max: the maximum value for the list

Precondition: max >= min is a number

"""

for x in range(len(alist)):

if alist[x] > max:

alist[x] = max

if alist[x] < min:

alist[x] = min

These are the tests:

def test_clamp():

"""

Test procedure for function clamp().

"""

print('Testing clamp()')

alist = [-1, 1, 3, 5]

result = funcs.clamp(alist,0,4)

introcs.assert_equals([ 0, 1, 3, 4],result)

introcs.assert_equals([-1, 1, 3, 5],alist)

result = funcs.clamp(alist,-2,8)

introcs.assert_equals([-1, 1, 3, 5],result)

introcs.assert_equals([-1, 1, 3, 5],alist)

result = funcs.clamp(alist,-2,-1)

introcs.assert_equals([-1,-1,-1,-1],result)

introcs.assert_equals([-1, 1, 3, 5],alist)

result = funcs.clamp(alist,1,1)

introcs.assert_equals([ 1, 1, 1, 1],result)

introcs.assert_equals([-1, 1, 3, 5],alist)

alist = [-1, 4, -1, 4, 2]

result = funcs.clamp(alist,0,4)

introcs.assert_equals([ 0, 4, 0, 4, 2],result)

introcs.assert_equals([-1, 4,-1, 4, 2],alist)

alist = [ 1, 3]

result = funcs.clamp(alist,0,4)

introcs.assert_equals([ 1, 3],result)

introcs.assert_equals([ 1, 3],alist)

alist = []

result = funcs.clamp(alist,0,4)

introcs.assert_equals([],result)

introcs.assert_equals([],alist)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!