I've got a script I'm writing, and have a function that should search a dictionary with this layout:
{
1 : ['name1','name2','name3','name4','name5'],
2 : ['name1','name2','name3','name4','name5']
}
for a word. Here's the function:
def find_student(list_curr):
''' (str, dict) -> list
Input is the student's name and the dictionary of lists that they should exist in; output is a list in the format:
[group,index]
'''
while True:
try:
stu_name = input("\nPlease enter the first or last name of the student, or press enter to exit:\n> ")
if not stu_name:
return False
else:
break
except ValueError:
print("Please enter a valid name")
for g,s in list_curr.items():
print("G",g)
print("S",s)
if any(stu_name in n for n in s):
# name was in group
print("\nFound a possible match: {} (group {}):".format(s[s.index(stu_name)],g))
pp.pprint(s)
if input("\nIs this the student you're looking for?\n> ") in "yesYES":
# found the correct student
print("Saving {} for group and {} for index.".format(g,s.index(stu_name)))
stu_info = [g,s.index(stu_name)]
return stu_info
# nothing was found
print("\n{} was not found in the list.".format(stu_name))
return False
When I run it, though, it breaks as soon as it finds a match. Everything below the if any():
part is not run, and it just returns without even printing the Found a possible match...
line. I've tried using the debugger in IDLE but it continually crashes whenever I open it. I've seen other posts really similar to this, but don't understand where I'm going wrong. Any ideas?
Edit: sorry, had for any()
, should be if any()
.