3 回答
TA贡献1802条经验 获得超4个赞
如果您希望用户重试该条目,您可以使用如下内容:
print("Team 1 player input")
elem = int(input("Amount of players: "))
for i in range(0, elem):
p = None
while not p or p in Team1_list:
p = input(f'Enter player number {num_team1} name (In batting order) eg J.Smith: ')
if p in Team1_list: print('That player already entered, please retry...')
Team1_list.append(p)
num_team1 = num_team1 + 1
print(Team1_list)
TA贡献1780条经验 获得超5个赞
如果您使用的是 Python 3.6+,您将受益于dict按键记住您的插入顺序。因此,您可以同时受益于list和。set考虑以下:
#Team 1 naming
print("Team 1 player input")
elem = int(input("Amount of players: "))
team = {}
while len(team) < elem:
num_team1 = len(team)+1
name = str(input(f'Enter player number {num_team1} name (In batting order) eg J.Smith: ')))
team[name] = 1
print(list(team.keys()))
如果您再次输入相同的名称,您的num_team1名称将不会增加,并且您的“列表”将忽略该重复的条目(即仅保留第一个条目的原始顺序)
添加回答
举报