I have asked this question previously but was advised that I included too much unnecessary code so I am going to ask with less code now and hopefully what I include is more on track.
I am trying to allow a member to leave the Team if they so wish. By doing this, the system will delete all their details from the system. I am receiving an error with my code. Can someone please advise what I am doing wrong and how this can be achieved?
I would like my add members and remove members to update all the time, based on user input and the needs of the members. I hope this makes sense!
Below is my code:
all_users = []
class Team(object):
members = [] # create an empty list to store data
user_id = 1
def __init__(self, first, last, address):
self.user_id = User.user_id
self.first = first
self.last = last
self.address = address
self.email = first + '.' + last + '@python.com'
Team.user_id += 1
@staticmethod
def remove_member():
print()
print("We're sorry to see you go , please fill out the following information to continue")
print()
first_name = input("What's your first name?\n")
second_name = input("What's your surname?\n")
address = input("Where do you live?\n")
unique_id = input("Finally, what is your User ID?\n")
unique_id = int(unique_id)
for i in enumerate(all_users):
if Team.user_id == unique_id:
all_users.remove[i]
def main():
user_1 = Team('chris', 'eel', 'london')
user_2 = Team('carl', 'jack', 'chelsea')
continue_program = True
while continue_program:
print("1. View all members")
print("2. Want to join the team?")
print("3. Need to leave the team?")
print("4. Quit")
try:
choice = int(input("Please pick one of the above options "))
if choice == 1:
Team.all_members()
elif choice == 2:
Team.add_member()
elif choice == 3:
Team.remove_member()
elif choice == 4:
continue_program = False
print()
print("Come back soon! ")
print()
else:
print("Invalid choice, please enter a number between 1-3")
main()
except ValueError:
print()
print("Please try again, enter a number between 1 - 3")
print()
if __name__ == "__main__":
main()
Lets focus on the removal code:
for i in enumerate(all_users):
if Team.user_id == unique_id:
all_users.remove[i]
enumerate
returns two values: the index and the object at the index. In your case, i
is a tuple
of those two values. .remove
is a function, not a collection so .remove[i]
will fail. Even then, its the wrong tool for the job: it scans the list for i
and removes it. You just want to delete. Finally, once you've changed the list, you need to stop the enumeration.
So, to clean this up:
for i, user in enumerate(all_users):
if user.user_id == unique_id:
del all_users[i]
break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.