-1

This function is only returning the value of 1. Can anyone help with this? The parameters describe what I am trying to implement and I am not sure why this is happening.

def clamp(alist,min,max):
    """
    MODIFIES the list so that every element is between min and max.

    Any number in the list, less than min is replaced with min.  Any 
    number in the list greater than max is replaced with max. Any number 
    between min and max is left unchanged.

    This is a PROCEDURE. It modifies alist, but does not return a new 
    list.

    Example: if alist is [-1, 1, 3, 5], then clamp(thelist,0,4) changes
    alist to have [0,1,3,4] as its contents.

    Parameter alist: the list to modify
    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
    """
    alist.append(min)
    alist.append(max)
    for a in alist:
        if max >= a >= min:
            return a
    alist.sort()
    return alist

Returns: 1, no matter input.

Adam.Er8
  • 12,675
  • 3
  • 26
  • 38
  • You can't string together conditional statements. Use `max >= a and a >= min` – user1558604 Jul 20 '20 at 02:47
  • In two separate lines or 1 because I tried 1 line and I still got the same response. And I wouldn't think it would be possible to use two lines for the if statement's statement. –  Jul 20 '20 at 02:53
  • 1
    what is the `thelist` keep the params consistent, it should be `clamp(alist, 0,4)` not `thelist` that isn't defined anywhere. you shouldn't have to append the `min` and `max` to the `alist`, if you are checking values within the already filled list, just compare the values instead of appending and then `replace()` the values and sort throught the list. – de_classified Jul 20 '20 at 02:57
  • 1
    sorry the list was an accident it should be `alist` instead but thats in the function description and not part of the code. That just refers to a hypothetical list that would already be defined in terminal. –  Jul 20 '20 at 03:05
  • 1
    If you are just creating a list with each element 'clamped' in a range, then don't understand the purpose of the `alist.append(min), alist.append(max) and alist.sort()` calls. Also, min and max are not good variable names since replaces built-in functions and so confusing the code reviewer. – DarrylG Jul 20 '20 at 03:12
  • @user1558604 Yes you can. Did you try it? However, whether that's what OP wants is another question. – wjandrea Jul 20 '20 at 03:19
  • Your code doesn't do what the description says at all. And I can get it to return values other than `1` by just changing its inputs. Please, go back to the drawing board, then if you're still having trouble, provide a [mre]. You might also want to read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/4518341) – wjandrea Jul 20 '20 at 03:25
  • Related: [Pythonic way to replace list values with upper and lower bound](https://stackoverflow.com/q/41329691/4518341) – wjandrea Jul 20 '20 at 03:44

1 Answers1

1

Three issues with code

  1. return a causes a return as soon as an element is within the range
  2. alist.append(min), alist.append(max), and alist.sort() are unrelated to stated desired functionality
  3. should not use built-in function names as variable names (i.e. min & max)

Code

def clamp(alist, min_, max_):
    for i, a in enumerate(alist):
        if a > max_:
          alist[i] = max_     # limit to max
        elif a < min_:
          alist[i] = min_     # limit to min

    return alist

print((clamp([-1, 1, 3, 5], 0, 4)))
# Output: [0, 1, 3, 4]

Alternative Code (using list comprehension)

def clamp(alist, min_, max_):
  return [min_ if a < min_ else max_ if a > max_ else a for a in alist]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • 1
    The first `if` statement check you have is redundant, it's not required especially if you just have `pass` in there. Skip right to the check of `if a > max_:` and `if a < min_:`. – de_classified Jul 20 '20 at 03:30
  • I would also add that procedures don't normally return anything, e.g. `random.shuffle` shuffles a list but doesn't return it. – wjandrea Jul 20 '20 at 03:32
  • 1
    @FishingCode I removed it :) Feel free to submit edits to code in answers yourself! – wjandrea Jul 20 '20 at 03:33
  • @FishingCode--kept the if condition and pass to keep as similar as possible to OP code to make it easier for he/she to follow my suggestions. Agree it's not necessary as the list comprehension version illustrates. – DarrylG Jul 20 '20 at 03:45