下面是从我前面的问题的更新版本在这里 。 我加入到代码中,如果get_close_matches名字是不是他们想要的人的名字,然后丢弃最接近的匹配,并重新运行该功能,第一抢第二最接近的匹配(现在,因为该函数将抛出从第一场比赛)。
你有关于如何能够更好地编写任何意见? 和工作。 >>
这是我到目前为止有:
def throwout(pickedName):
employeeNames.remove(pickedName)
pickedName = difflib.get_close_matches(userEmpName, employeeNames, 1)
print(pickedName)
userNameOK = input("Is this the name of the person you're looking for?\n\n Type 'Y' or 'N'.\n\n")
employeeNames = ['Colton','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
userEmpName = input("Please enter the employee name you're searching for. We'll return the best match on record.")
pickedName = difflib.get_close_matches(userEmpName, employeeNames, 1)
print(pickedName)
userNameOK = input("Is this the name of the person you're looking for?\n\n Type 'Y' or 'N'.\n\n")
if userNameOK == "N" or "n":
if pickedName in employeeNames:
throwout(pickedName)
else:
break
else:
break
错误运行了在列表中的名称:
Traceback (most recent call last):
File "C:/Python33/employee1.py", line 64, in <module>
print(userAnswer + " is the right choice.\n")
NameError: global name 'userAnswer' is not defined
据我所知,意味着因为名字的名单是消除他们根本没有更多的名字,全局变量“userAnswer”然后不确定的。