struggling with the optional homework

GuidoG93 (970 points)
4 9 13
asked Oct 26, 2021 in HW1 optional by GuidoG93 (970 points)
recategorized Nov 1, 2021 by andrea.sterbini
Hello everyone, I was trying to do the optional homework but it seems like I cannot get the result I want.

Basically I would like to get my list to delete the first element of itself if it doesn't fit my request (basically, an else statement), and return it without that element.

I developed a function for it, but it seems like it works only once and I can't find a way to let my program repeat this function until the list doesn't have any elements in. I tried using len to get the number of elements in the list too.

Can someone help me understand what I'm doing wrong?

5 Answers

GabrielAlexandru (7760 points)
2 4 29
answered Oct 26, 2021 by GabrielAlexandru (7,760 points)
Without the code is a little hard to understand. But the forum rules don’t allow us to share code.

So the first thing that comes to mind is: did you check if the condition is always verified or if there are some case when it returns false?
GuidoG93 (970 points)
4 9 13
commented Oct 26, 2021 by GuidoG93 (970 points)
The function that deletes the first element of the list works, in fact if I write it again and again it works as I wanted, the thing is that it seems that I can't find a way to make it repeat itself until the end of the list.
andrea.sterbini (172780 points)
514 935 1789
commented Oct 26, 2021 by andrea.sterbini (172,780 points)
have you tried a for loop on a list copy?

have you tried a recursive function?
GuidoG93 (970 points)
4 9 13
commented Oct 26, 2021 by GuidoG93 (970 points)
I  tried but every time it gives me various errors like: IndexError: list index out of range
Luigi Pizza (6120 points)
11 20 65
answered Oct 26, 2021 by Luigi Pizza (6,120 points)
I've done something similar in my script. There's no need for a separate function, you can just write

Sum -= l.pop(0)

So that the accumulator that counts the sum of the elements in your sequence is reduced be it's first element.

Another thing. You are probably using a counter in this exercise to consider where your function is. When you do the .pop or delete the first element in any other way, you obviously have to modify that counter. Otherwise your function will skip an element  (If you need an example to understand better let me know). So this becomes:

Acc -= 1

Last thing. If you still have some problems with your script it may be because it gets to the end, but the condition you have put into the while statement doesn't get false. A solution, though not much elegant, is to append a value bigger than the number bigger than the subtotal.

Let me know if this answer was useful and, if it was, pin it as the best. Thank you!
GuidoG93 (970 points)
4 9 13
commented Oct 26, 2021 by GuidoG93 (970 points)
Hi, thanks for the answer. In the first case, I tried to use already l.pop(0), but I have no idea why it doesn't return me the whole list but just an integer. I was confused by it too but I couldn't find  a way to fix it
Luigi Pizza (6120 points)
11 20 65
commented Oct 26, 2021 by Luigi Pizza (6,120 points)
L.pop(n) cancels the element in the n+1th position and returns it. So, by doing:

Acc -= l.pop(0)

l becomes the list without that element in the 0th position and the accumulator is reduced by that value. So, in some way, it returns both the list without that integer and that integer itself. Makes sense?
Excale (490 points)
0 0 6
answered Oct 26, 2021 by Excale (490 points)
If I understood what you're trying to do, python already has a function to remove a certain element from the list, given an index. You should try using that, you should be able to find it quickly by searching on google.

The function operates on the list itself, so you can continue using the variable the list was in.
1899325 (5730 points)
0 0 15
answered Oct 27, 2021 by 1899325 (5,730 points)

As stated from the other students in the answers, a useful thing is the pop() method.

I think this webpage can help in understanding the pop() function better:

Nilats (3750 points)
7 14 29
answered Oct 27, 2021 by Nilats (3,750 points)
Have you tried writing a simpler version of your loop and print() what you want to do, having something visual helps u understand better your own code.