Patrick B. answered 11/19/23
Industry data scientist / math PhD / 6 years teaching experience
The problem is that you use the variable name "valid_passport" inside the for loop later in the function main. This means that within the method "main" "valid_passport" is a local variable that is different from the defined valid_passport method. The reason you are getting the "referenced before assignment" error is that you run the following line before the assignment of valid_passport (in the for loop).
valid_passports = [pp for pp in passports if valid_passport(pp)]
Solutions here are to change the name of the valid_passport method or to change the name of the variable valid_passport used in the for loop.