debug this function in python:
async def scheduled_results(update: Update, context: CallbackContext):
    try:
        while True:
            sum = get_results()[-1]
            if sum:
                all_ids = await get_ids()
                ids = []
                for id in all_ids:
                    ids.append(id[0])
                for user in ids:
                    try:
                        await context.bot.send_message(chat_id=user, text=f"INFO at {datetime.now().strftime('%m/%Y %H:%M ')} \n Time limit for round {counter} is reached, prepare for the next level")
                    # os.kill(pid, signal.SIGTERM)
                    except Exception as e:
                        print(e)
                        traceback.print_exc()
                        pass
                await rank()
                sum = sheet.values().get(spreadsheetId=SheetID, range=f'Sheet{counter - 1}!C1:C1').execute()
                sum = sum.get('values', [])
                highest = None
                for lst in sum:
                    if highest is None or (lst[-1]) > (highest[-1]):
                        highest = lst
                for user in ids:
                    try:
                        await context.bot.send_message(chat_id=user, text=f"RESULTS at {datetime.now().strftime('%m/%Y %H:%M ')} \n THE WINNER OF ROUND {counter - 1} IS {highest[1]} WITH  {highest[-1]} POINTS \n {emoji_clapping_hands * 3} {emoji_party_popper * 2} {cheers_emoji * 2}")
                    except Exception as e:
                        print(e)
                        pass
                if counter == 6:
                    lb = sheet.values().get(spreadsheetId=SheetID, range=f'Leaders!A2:C100').execute()
                    lb = lb.get('values', [])
                    sorted_list = sorted(lb, key=lambda x: int(x[-1]), reverse=True)
                    sorted_list = "\n".join([f"[{', '.join(sublist)}]" for sublist in sorted_list])
                    for user in ids:
                        try:
                            await context.bot.send_message(chat_id=user,
                                                           text=f"RESULTS at {datetime.now().strftime('%m/%Y %H:%M ')} \n THE OVERALL RANKING ON ALL ROUNDS IS {counter - 1} ARRANGED IN DECENDING ORDER  {sorted_list} \n {emoji_clapping_hands * 3} {emoji_party_popper * 2} {cheers_emoji * 2}")
                        except Exception as e:
                            print(e)
                    os.kill(pid, signal.SIGTERM)
            else:
                break
  Please reply with exactly 6 paragraphs and separate each paragraph with two new lines. The very first paragraph must start with the word "activity".
user