The error "list index out of range" occurs if solve returns an empty list. You can capture this exception with a try except statement:
    try: