Reputation: 9
I'm recently coding a bot for a small server between friends. I'm trying to add a quiz minigame to the bot, but ran into a problem.
Here's the code I have:
@bot.command(aliases = ['popquiz', 'trivia'])
async def quiz(ctx, *, arg):
if arg == 'league':
QA = leagueQA
else:
await ctx.send('The quiz you asked for is not yet available.')
return
Q = random.randrange(len(QA))
await ctx.send(QA[Q][0])
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
answer = await bot.wait_for('message', check=check, timeout=20)
print(answer.content.lower())
if (substring in answer.content.lower() for substring in QA[Q][1]):
await ctx.send('Close but not quite.')
return
elif (substring in answer.content.lower() for substring in QA[Q][2]):
await ctx.send('Correct!')
return
else:
await ctx.send('Incorrect')
return
leagueQA is essentially a nested list in the following format:
leagueQA = [
['Question 1', ['close answer1', 'close answer2'], ['right answer']],
['Question 2', ['close answer1', 'close answer2'],['right answer1','right answer2']],
['Question 3', ['close answer'], ['right answer']]]
Now when I run the bot and type in $quiz league, the bot will ask me a random question from the list, and when I answer the question, no matter what I say, it will respond with the first response: Close but not quite.
I tried printing the answer to make sure it does take in the correct answer. I've double-checked my nested list and it seems fine. I've tried many different questions and responses, and the results are all the same. Even with a completely wrong answer like 'fadsfcasjdl' the first if condition is still true. I've also tried using substring == answer.content.lower(), using contains, and removing the returns, nothing seems to work. Pls send help.
Upvotes: 0
Views: 84
Reputation: 3120
You have to check any
of the tuple.
A non empty tuple is True
even if it contains False
.
Try it out:
> bool((False))
True
> bool((True))
True
> bool(())
False
So you need to change to:
if any(substring in answer.content.lower() for substring in QA[Q][1]):
await ctx.send('Close but not quite.')
return
elif any(substring in answer.content.lower() for substring in QA[Q][2]):
await ctx.send('Correct!')
return
else:
await ctx.send('Incorrect')
return
You can then adjust for exact or partial matches if you want.
Upvotes: 3