Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据用户输入从python中的类中删除项?_Python_Class - Fatal编程技术网

如何根据用户输入从python中的类中删除项?

如何根据用户输入从python中的类中删除项?,python,class,Python,Class,我试图允许一名队员离开球队,如果他们愿意的话。通过这样做,系统将删除他们,他们的用户ID将可供下一个希望加入团队的人使用。我收到一个代码错误。有人能告诉我我做错了什么以及如何做到这一点吗 我希望我的“添加成员”和“删除成员”能够根据用户输入和成员的需要随时更新。我希望这是有意义的 例如:如果“Carl”决定离开,他将被删除,下一个要加入的成员将被分配成员ID“2” 下面是我的代码: all_users = [] class Team(object): members = []

我试图允许一名队员离开球队,如果他们愿意的话。通过这样做,系统将删除他们,他们的用户ID将可供下一个希望加入团队的人使用。我收到一个代码错误。有人能告诉我我做错了什么以及如何做到这一点吗

我希望我的“添加成员”和“删除成员”能够根据用户输入和成员的需要随时更新。我希望这是有意义的

例如:如果“Carl”决定离开,他将被删除,下一个要加入的成员将被分配成员ID“2”

下面是我的代码:

all_users = []


class Team(object):
    members = []  
    user_id = 0

    def __init__(self, first, last, address):
        self.user_id = Team.user_id
        self.first = first
        self.last = last
        self.address = address
        self.email = first + '.' + last + '@python.com'
        Team.user_id += 1

        Team.members.append(self)

    def __str__(self):
        print()
        return 'Membership ID: {}\nFirst Name: {}\nSurname: {}\nLocation: {}\nEmail: {}\n'.format(self.user_id,
                                                                                          self.first, self.last,
                                                                                          self.address,
                                                                                          self.email)
        print()

    @staticmethod
    def all_members():
        for user in all_users:
            print(user)

    @staticmethod
    def add_member():
        print()
        print("Welcome to the team!")
        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")

        all_users.append(Team(first_name, second_name, address))

    @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")

        if unique_id in all_users:
            del unique_id

        all_users(User(first_name, second_name, address))


    def main():
        user_1 = Team('Chris', 'Parker', 'London')
        user_2 = Team('Carl', 'Lane', 'Chelsea')

    all_users.extend([user_1, user_2])

    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("3. 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()

remove_member方法是错误的,原因有几个。行
del unique\u id
不会从
all\u users
中删除值,这只是团队成员的列表。而且你不应该要求用户提供所有这些信息——只要ID(或名称)就足够了

我的建议是:

@staticmethod
def remove_member():
    print()
    print("We're sorry to see you go , please fill out the following information to continue")
    print()
    unique_id = input("what is your User ID?\n")
    unique_id = int(unique_id)
    for i, user in enumerate(all_users):
        if user.user_id == unique_id:
            all_users.remove(i)
            break

如果重用用户ID很重要,则可以保留一个再次可用的用户ID列表。创建新成员时,可以先选中该列表,如果有旧id,则使用旧id

您还可以选择不重用已删除用户的用户ID。id并没有真正的意义,不重复使用它会让它对你来说更简单

最后,您可能需要重新构造代码:

Team(first_name, second_name, address)
这毫无意义:一个有名字、姓氏和地址的团队!最好有两个班:

team = Team()
user = User(first_name, second_name, address)
team.add_member(user)
team.remove_member(user)
其他一些提示:

# This won't work, because all_users contains Team instances, not numbers. So the user will never be found.
if unique_id in all_users:
    # This won't do anything: `del` only removes the access to the variable (might free up memory in heavy applications). It doesn't remove the user from all_users
    del unique_id

欢迎来到SO。请阅读。你能把你的代码裁剪成相关的摘录吗?代码仍然需要单独运行,但我们不需要(例如)20个
print
语句和列出的每个方法。您的缩进到处都是。您发布的代码无法运行。谢谢您的帮助。当我尝试实现此功能时,我得到了以下错误代码:all_users.remove[I]TypeError:“builtin_function_或_method”对象不可订阅抱歉,我的错误-用于编写
(I)
,而不是
[I]
。固定的。您还可以使用
删除所有用户[i]