这是我上一个问题here的更新版本。我在代码中添加了这样一个代码,如果get-chu-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
列表中的名称用完时出错:
^{pr2}$我理解这意味着,由于名称列表中没有更多的名称可以删除它们,那么全局变量“userAnswer”就没有定义了。在
不需要创建函数来抛出列表中的名称,因为
list.remove(name)
在一行中执行相同的操作。在但是,使用全局变量通常是不好的做法,因此您可以创建一个函数来完成所有这些操作并返回正确的
Answer
另外,由于
employeeNames
在每次从中删除一个名称时都会被修改,因此最好创建一个列表的副本并处理该特定列表相关问题 更多 >
编程相关推荐