我正在学习python,我正在尝试为足球队创建一个课程。我正在处理python的不同方面,以尝试更好地理解。我想要一个基于菜单的系统,该系统允许程序的用户查看所有当前团队成员或者注册加入团队。每个团队成员都有一个唯一的ID,当一个人加入时,它将简单地给他们下一个可用的号码。例如,Chris的唯一ID为1,Carl有2,所以如果Joe加入,他将自动获得第三名。以下是我目前的代码。观看卡片播放器对我来说非常适合,但我正在努力添加新播放器。
所以我的问题是,如何将新成员添加到我将存储所有数据的空列表中,如何实现下一个加入团队的人员将获得下一个可用的号码?这是为了确保每个成员编号都是唯一的。在此先感谢,渴望学习!
class Team(object):
members = [] #create an empty list to store data
def __init__(self, user_id, first, last, address):
self.user_id = user_id
self.first = first
self.last = last
self.address = address
self.email = first + '.' + last + '@python.com'
#instance is fully initialized so I am adding it to the list of users
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 Team.members:
print (user)
def add_member(Team):
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")
for x in Team:
unique_id = unique_id =+ 1
user[user_id] = [user_id, first_name, second_name, address]
user_1 = Team(1, 'Chris', 'Parker', 'London')
user_2 = Team(2, 'Carl', 'Lane', 'Chelsea')
def menu(object):
continue_program = True
while continue_program:
print("1. View all members")
print("2. Add user")
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(object)
elif choice == 3:
continue_program = False
print()
print("Come back soon! ")
print()
else:
print("Invalid choice, please enter a number between 1-3")
menu(object)
except ValueError:
print()
print("Please try again, enter a number between 1 - 3")
print()
#my main program
menu(object)
答案 0 :(得分:0)
你是如此亲密:
@staticmethod
def add_member(): # belongs to the class
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")
# get the maximum id and add one
if Team.members:
unique_id = max(Team.members, key=lambda m: m.user_id) + 1
else:
unique_id = 0
Team(unique_id , first_name, second_name, address)
在主要功能改变中:
elif choice == 2:
Team.add_member()
答案 1 :(得分:0)
事情发生了变化:
如果您不明白,请发表评论
all_users = []
class Team(object):
members = [] # create an empty list to store data
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
# instance is fully initialized so I am adding it to the list of users
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")
# for x in Team:
# unique_id = unique_id = + 1
all_users.append(Team(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. Add user")
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:
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()