如何阻止用户使用相同的输入?

时间:2018-10-29 00:14:01

标签: python

如何阻止用户为团队成员使用相同的确切名称?

for i in range(teamMembers):
    while True:
       try: 
         teamMember = input("Enter the name of team member {} :".format(i+1))


      if not teamMember:
         raise ValueError("Team member can not be blank")

      elif teamMember.isdigit():
         raise ValueError("Team member name can not be a number")

                    else: 
                        break

                except ValueError as e:
                    print (e)

1 个答案:

答案 0 :(得分:1)

members = []
for i in range(teamMembers):
    teamMember = input("Enter the name of team member {} :".format(i+1))
    if not teamMember:
        raise ValueError("Team member can not be blank")
    if teamMember in members:
        raise ValueError("This name already exist")
    members.append(teamMember)

我认为这就是您想要的-如果在try块中什么都不会失败,但是您仍然想raise例外,则可以直接raise