如何阻止用户为团队成员使用相同的确切名称?
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)
答案 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
。