指定 return 时函数不起作用

Function does not work when return is specified

我正在尝试创建一个函数,使用 telethon 库从电报中获取用户的联系人列表,在获取联系人后,它将提取每个联系人的 user idfirst name contact 然后它将比较联系人的 first name 和用户输入的名称,如果相似,它将发送用户输入的消息。该函数在将消息成功发送到预期联系人时起作用,但是当我使用 return 语句时它不起作用

async def sendUserMessage(self, response, message):
    client = TelegramClient('session', api_id, api_hash)
    await client.connect()
    result = await client(functions.contacts.GetContactsRequest(
        hash=0
    ))
    for user in result.users:
        try:
            s = SequenceMatcher(None, response, user.first_name)
            print(s.ratio())
            print(response)
            print(user.first_name)
            if s.ratio() > 0.75:
                print(response, user.id)
                receiver = InputPeerUser(user.id, 0)
                await client.send_message(receiver, message, parse_mode='html')
                return print("Sent a message successfully")
            else:
                return print("Could not send message")

        except Exception:
            pass
    await client.disconnect()

a = input("Who do you want to send the message to? ")
b = input("What do you wanna send? ")

async def main():
    await Methods().sendUserMessage(a, b)

asyncio.run(main())

这里的a是我想要消息发送给的人,b是消息。 任何帮助将不胜感激

更新

我找到问题了,Return 语句在满足条件之前就中断了循环。

有没有办法在满足条件时return只声明一次?

我使用了布尔标志,但它仍然不起作用,我不确定我是否以正确的方式使用它

async def sendUserMessage(self, response, message):
    isSent = False
    client = TelegramClient('session', api_id, api_hash)
    await client.connect()
    result = await client(functions.contacts.GetContactsRequest(
        hash=0
    ))
    for user in result.users:
        try:
            s = SequenceMatcher(None, response, user.first_name)
            if s.ratio() > 0.75:
                receiver = InputPeerUser(user.id, 0)
                await client.send_message(receiver, message, parse_mode='html')
                isSent = True
            else:
                isSent = False
        except Exception:
            pass
    await client.disconnect()
    if isSent:
        print("Message sent successfully")
    else:
        print("Message could not be sent") 

我能够通过使用 boolean flag 而不是在 for 循环中使用 print 来解决问题

async def sendUserMessage(self, response):
        isSent = False
        client = TelegramClient('session', api_id, api_hash)
        await client.connect()
        result = await client(functions.contacts.GetContactsRequest(
            hash=0
        ))
        for user in result.users:
            try:
                s = SequenceMatcher(None, response, user.first_name)
                if s.ratio() > 0.75 or distance.levenshtein(response, user.first_name) < 3:
                    speak("What do you wanna send?")
                    message = takeCommand()
                    isSent = True
                    receiver = InputPeerUser(user.id, 0)
                    await client.send_message(receiver, message, parse_mode='html')
                else:
                    pass
            except Exception:
                pass
        await client.disconnect()
        if isSent:
            speak("Message sent successfully")
        else:
            speak("Could not find that user in your contacts")