inspired by exercise 31 of Learn Python the Hard Way