Solution to a problem with Python