Math and logic problems in Python