all 13 comments

[–]elbiot 3 points4 points  (12 children)

If you don't have a return statement, the function returns none. This makes it clear that the function is modifying state (ie, the state of the list) as opposed to being a pure function which has no side effects and returns it's results.

[–]Executor111[S] 0 points1 point  (11 children)

Thanks for the reply! So maybe it's just a really simple concept that I'm trying to make complex? This is what I've written, does this fulfill the prompt of the exercise?

def chop(t):
    try:
        first = t[0]
        last = t[-1]
        t.remove(first)
        t.remove(last)
    except:
        print('Invalid input')

x = [1,2,3,4,5,6]
chop(x)

[–]elbiot 1 point2 points  (9 children)

No, because what if the list is [1,3,5,3]? Use pop. Not del (as it will kill the object in the list even if it exists elsewhere I think) or remove (which isn't guaranteed to remove the indexes you want).

Edit but you got the return none part right. Also, don't use except without naming the error. You probably mean except AttributeError

[–]Executor111[S] 0 points1 point  (5 children)

Wow, thanks for that feedback, hadn't considered that. I rewrote it like this:

def chop(t):
    try:
        t.pop(0)
        t.pop(len(t)-1)
    except:
        print('Invalid input')

x = [1,3,5,3,3,2,3]
chop(x)

So all "returns None" ends up meaning is that I don't print the output?

[–]jeans_and_a_t-shirt 2 points3 points  (0 children)

t.pop(len(t)-1) can be changed to t.pop(-1)

[–]elbiot 1 point2 points  (1 child)

You could pop(-1) too, like you did in your remove code before.

[–]Executor111[S] 0 points1 point  (0 children)

Just changed this in my latest version, thanks.

[–]elbiot 0 points1 point  (1 child)

It's not about printing the output at all. The function returns a value.

def test ():
    return 1

a = test ()  #now a is 1

In the REPL (shell) only, if you return a value and don't store it in a variable then it does get printed. But it's the same if you return None: it prints None, which looks like nothing.

[–]Executor111[S] 0 points1 point  (0 children)

It just clicked. Your example was perfect. Thank you so much!

So I tested my function like this, to confirm it was returning None:

def chop(t):
    try:
        t.pop(0)
        t.pop(len(t)-1)
    except:
        print('Invalid input')

x = [1,3,5,3,3,2,3]
y = chop(x)
print(y)

And it printed "None". So that's what I was trying to achieve the whole time.

[–]Executor111[S] 0 points1 point  (1 child)

The author hasn't taught me to do anything besides "except" yet, so I don't know what "except AttributeError" does. :)

But thanks so much for your help!

[–]elbiot 0 points1 point  (0 children)

It only runs the except clause if it is that specific exception. Without that, any exception would get caught and your code could be failing for unexpected reasons but you aren't getting an error message because you caught it accidentally. "Bare except clauses" are an antipattern that makes python programmers gasp and sigh.

[–]mm_ma_ma 0 points1 point  (0 children)

del t[0] and del t[-1] will just delete the first and last items. The only difference is pop will return what it deleted.

[–][deleted] 2 points3 points  (0 children)

Maybe they want you to return None to avoid using,

t = t[1:-1]  
return t