Python logic building question and solution