Hello ktfank,
There are three ways to run the script:
1. save the code as .py file and execute via Tools Menu -> Run script command,
2. turn on Rich Python Console in Advanced Preferences tab, then copy the full code there and press Enter a couple of times, while the cursor is in the very end of the last code line,
3. in the simple Console run the lines one by one (copy line, press Enter and etc.), in this case note that indentation is important for the contents of the for loop and if statement, so these lines should start with the corresponding number of "tabs".