voted = {}
创建一个散列表
def check_vote(name):
if voted.get(name): //get方法检测name是否在列表中 在就返回True 不再就返回False
print(“Let he go out”)
else: //因为此处是False 所以name不存在在散列表中 因此可以允许投票 设置散列表中 voted[name]=True
voted[name] = True
print("let he go to vote ")
check_vote(“Tom”)
check_vote(“Tom”)
完整代码
voted = {}
def check_vote(name):
if voted.get(name):
print("Let he go out")
else:
voted[name] = True
print("let he go to vote ")
check_vote("Tom")
check_vote("Tom")
测试结果
let he go to vote
Let he go out