Rich Biggs
Rich Biggs

Reputation: 17

Python input interpretation as string

I am writing a discord python bot.

I am trying to create a guessing style command for primary colors.

However I am receiving a TypeError: 'str' object is not callable after the user submits their guess.

I was able to solve this with an integer guessing style command with m.content.isdigit() however I am hung up on solving this.

I have tried adding str() containers around several areas with no joy.

elif user_message.lower().startswith('!guesscolor'):
    await message.channel.send('Guess a color of a rainbow')

    def is_correct(m):
        return m.author == message.author and m.content()
    colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo,', 'violet']
    answer = random.choice(colors)
    print(f'The correct color is: {answer}.')

    try:
        guess = await client.wait_for('message', check=is_correct, timeout=5.0)
    except asyncio.TimeoutError:
        return await message.channel.send(f'Sorry, you took too long it was {answer}.')
    if (guess.content) == answer:
        await message.channel.send('You are right!')
    else:
        await message.channel.send(f'Oops, it was actually {answer}.')
        return
    return

Any input or direction to solve this is greatly appreciated.

Below is the working version for the integer guess:

elif user_message.lower().startswith('!guessnumber'):
   await message.channel.send('Guess a number between 1 and 10')
   def is_correct(m):
       return m.author == message.author and m.content.isdigit()
   answer = random.randint(1,10)
   print(f'The correct answer is: {answer}.')

   try:
       guess = await client.wait_for('message', check=is_correct, timeout=5.0)
   except asyncio.TimeoutError:
       return await message.channel.send(f'Sorry, you took too long it was {answer}.')
   if int(guess.content) == answer:
       await message.channel.send('You are right!')
   else:
        await message.channel.send(f'Oops, it was actually {answer}.')
        return
   return

Upvotes: 0

Views: 93

Answers (1)

Barmar
Barmar

Reputation: 782097

If you want to check that it's one of the possible colors, do this:

   def is_correct(m):
       return m.author == message.author and m.content in colors

Upvotes: 1

Related Questions