Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove object
#1
Can any1 help me with my code? I cant remove object inside of my list. And how to update them.

import pickle
from module2 import cmod2
plannerlist = []

class cmod3:
    def meth31():
        planner = []
        add = cmod2.meth22()
        planner.append(add)
        plannerlist.append(planner)
        pickle.dump(plannerlist, open('pl','wb'))
        print('Your plan:')
        for planner in plannerlist:
            print(*planner)
        return  planner
    #return plannerlist

    #      print(pl)
#show

    def meth32():
        plannerlist = pickle.load(open("pl", "rb"))
        print("Current plan list:")
        for planner in plannerlist:
            print(*planner)
        removeplan = input("Which plan would you like to remove?")
        for planner in plannerlist:
            if planner == removeplan:
                plannerlist.remove(planner)

        pickle.dump(plannerlist, open("pl", "wb"))
        print('Plan removed.')
#remove
Reply
#2
What happens when you try to run meth32? And is there some reason you didn't name it remove_plan, or something more descriptive than meth32?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It shows all the object in the list but when I try to remove some of it, nothing happen.
Reply
#4
Then I am guessing your equality is failing on line 28. From line 25 planner appears to be an iterable, maybe either a list or a tuple. But the equality is comparing the string from the input on line 26 to the iterable, which is never going to match.

Edit: If one of the items in the iterable is a string that is meant to match the input, you need to compare to that, not to the whole iterable.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I try to put 'str' on removeplan and still nothing happens, what I am missing?
Reply
#6
You are comparing a string to a list. '5' is never going to equal [1, 2, 5]. Even if you make a string of the list, you are now comparing '5' to '[1, 2, 5]', which is still not a match, unless you type in the list literal exactly.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to destroy or remove object rendered with opengl from the screen? hsunteik 1 7,052 Apr-09-2017, 01:30 PM
Last Post: hsunteik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020