How to search a list item and return in a message?

Asked

Viewed 65 times

0

I’m starting programming, and I’m doing a bot on Discord using python (Discord.py)

I need to create a command for the bot to return the availability of fruit within the list.

class Frutas(commands.Cog):  # cog 

    def __init__(self, client):  # setup pra cog.
        self.client = client

    @commands.command(aliases=['f'])  # comando para escolher frutas.
    async def fruta(self, ctx, *, fruta):
        responses = [  # frutas disponíveis
            'Maçã vermelha',
            'Maçã verde',
            'Banana amarela'
        ]
        if fruta in responses:  # se a fruta solicitada existe em "responses":
            await ctx.channel.send(responses.index)  # (aqui deveria mandar a fruta correspondente à lista.)
        else:  # se não:
            await ctx.channel.send(f'não temos essa fruta na loja ({fruta})')  # retorne que não temos a fruta.


def setup(client):
    client.add_cog(Frutas(client))  # cog

with this code, it returns me this

print do bot

I wanted to put ! f apple and make the bot identify the "Red Apple" on the list and return it as a message of my choice, but I don’t know how to make him identify it...

I have also tested without the index, but then it sends the list with all the fruits, and I still need to type "! f Red apple" instead of "! f apple"

2 answers

0

His most immediate mistake is that index is a method of list. When you use responses.index as a value for the method ctx.channel.send(...), you’re just sending the method index, and not an invocation of the method. That’s why he always answers with <built-in method index of list object at ...>, because this is the pointer (location in memory) of the method itself.

Your code, therefore, is working fine; you just need to change the value sent back, from responses.index to the message you want to return, such as f'Aqui está sua {fruta}!'

0

The basic logic is, as soon as the code is called it iterate the list of possible fruits and test whether the target fruit is contained in the current fruit.

Important points:

  1. Accentuation and cedilla
    Avoid accentuation as 'Maca' in 'Maçã vermelha' returns false, so to circumvent this, it may be interesting avoidance accentuation and the 'c' with the cedilla

  2. Use the method .upper() The method upper() converts all the letters of the string into UPPERCASE, this is important because the in is case sensitive, so the code banana in Banana returns false.

frutas = ['MACA VERMELHA', 'MACA VERDE', 'BANANA AMARELA']

frutaAlvo = 'MacA'
resultado = list()

for fruta in frutas:
    if frutaAlvo.upper() in fruta.upper():
        resultado.append(fruta) # Com apenas esse trecho você terá as possíveis repostas.
    #Com o código abaixo salva o index da primeira ocorrência "mínima":
        #indice = frutas.index(fruta)
        #break


print(resultado)

Output: ['MACA VERMELHA', 'MACA VERDE']

  • but how would I turn the fruitAlvo into the fruit the user chooses? (! f (fruit)

  • The target fruit is just an example string that I used to explain the code. But, whereas you have the exact string that is contained in a string list, you can use listDeString.index('string'), which will return the index the string is in.

  • What I found confusing is that in your question there are two problems: You want to know how to get the possible answers to the user input (since the apple input can be both red and green apple), and the problem with the content. For the first problem, you can use the break command inside if you only want one answer, and for the second the previous comment.

  • If my code was helpful, please consider evaluating it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.