-2

I am trying to make a method which will remove item from list, but when I am trying to choose which one I want remove I get error

(TypeError: rent() takes 1 positional argument but 2 were given)``` 
account.rent(2)

When I leave it with no id account.rent(), then method removes 2 first items from list.

Method:

def rent(self):
        if(self.logged==True):
            for id in lib.bib.BookList:
                lib.bib.BookList.remove(id)
        else:
            print("No Access")

List:

library.BookList.insert(0,"Book n1")
library.BookList.insert(1,"Book n2")
library.BookList.insert(2,"Book n3")

account.rent() and list is in main.py, def rent(self) in account.py.

I don't know where I declared removing 2 arguments.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
NIKEN
  • 19
  • 5
  • 2
    `rent` is defined with only one argument: `self`, the instance that it belongs to, which is passed implicitly. Why do you expect it to take two? Also, it's not a good idea to delete items from a list that you are iterating over – Pranav Hosangadi Jun 22 '21 at 19:40
  • 1
    What would you expect the `2` in `account.rent(2)` to be used for? The method doesn't make any use of any such parameter. – Thierry Lathuille Jun 22 '21 at 19:41
  • I was trying to add other argument like ```def rent(self,bib)```,```def rent(self,lib)``` or even ```def rent(self,BookList)``` but with same result – NIKEN Jun 22 '21 at 19:42
  • @ThierryLathuille ```account.rent(2)``` should, in my opinion, remove second element from BookList – NIKEN Jun 22 '21 at 19:43
  • To be honest, I am trying to make method which will be removes x element from A list and add x element to B list – NIKEN Jun 22 '21 at 19:45
  • You shouldn't remove from lists while iterating them, anyway - https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating – OneCricketeer Jun 22 '21 at 19:59
  • _trying to add other argument like..._ Those are all correct, if you want to give a parameter to the function, so what **other** error did you get? – OneCricketeer Jun 22 '21 at 20:00
  • No more errors, just only one "TypeError: rent() takes 1 positional argument but 2 were given" – NIKEN Jun 22 '21 at 20:08

3 Answers3

1

don't know where I [made it] remove 2 arguments.

Because you have a list of length 3, and removed all but one because that's how the for loop works.

I don't think you need a loop, but see this post about how to properly do that - How to remove items from a list while iterating?

account.rent(2) should, in my opinion, remove second element from BookList

If that is want you want, then use del

def rent(self, position):
    if self.logged and (0 < position < len(lib.bib.BookList)):
        del lib.bib.BookList[position]
    else:
        print("No Access")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I know how to slice the list, but I have no idea what howMuch is? – NIKEN Jun 22 '21 at 20:09
  • "You should slice the list if you want to remove the first few items, not loop over it" in account.rent() i want to remove only one item, chosen by me by id in list – NIKEN Jun 22 '21 at 20:11
  • `howMuch` is a parameter name I chose based on what you wrote in the question. If you want to remove "all elements with an `ID==x`", then you have no `ID` field in what you've inserted in the `BookList`... If you want to remove **an index**, then that would be `del lib.bib.BookList[position]` – OneCricketeer Jun 22 '21 at 20:14
  • That ``` (0 < position < len(lib.bib.BookList))``` helped me a lot. Thanks. – NIKEN Jun 22 '21 at 20:35
0

try this:

def rent(self, book):
    if(self.logged==True):
        if book in self.lib.bib.BookList:
            self.lib.bib.BookList.remove(book)
    else:
        print("No Access")

edited

George Poliovei
  • 1,009
  • 10
  • 12
  • 1) This assumes that equality checks against the list elements are enforced 2) `.remove()` will work even without the `if` check. – OneCricketeer Jun 22 '21 at 20:17
0

In your rent function, in the remove loop, your array is changed after each remove call. this function remove even element from your array. assume a = ['n0', 'n1, 'n2', 'n3', 'n4', 'n5']

in first iteration first element of the array is remove and after first remove call, first element is remove and we have a = ['n1', 'n2', 'n3', 'n4', 'n5']

in second iteration second element of array a (['n1', 'n2', 'n3', 'n4', 'n5']) is remove and after second remove call, we have a = ['n1', 'n3', 'n4', 'n5'].

Remove an item from a list in Python (clear, pop, remove, del)

1- Remove all items: clear()

2- Remove an item by index and get its value: pop()

3- Remove an item by value: remove()

4- Remove items by index or slice: del.

5- Remove items that meet the condition: List comprehensions.