Search code examples
pythondiscorddiscord.py

Discord.py 1.7 random choice is not working


I'm coding a roulette command on Discord.py 1.7, and I have it choosing a number, and then seeing if the number is even or odd. If I choose even when doing the command, and the number chosen by the code is even, it sends a win message. Same thing for when they are both odd. The problem occurs when I choose even and the code chooses odd. When that happens it still shows the win message. I am even more confused because when I choose odd and the code chooses even, it shows the lose message. here is my code

choices = ['odd','even']
@slash.slash(description='A staple of gambling')
async def roulette(ctx,bet:int,choice):
  if choice == int:
    await ctx.send('Sorry, you cannot bet on singular numbers yet. this will be added soon')
  elif any(word in choice for word in choices):
    q = await ctx.send('Bets are in place!\nresults revealed in: 3 seconds')
    await asyncio.sleep(1)
    await q.edit(content='Bets are in place!\nresults revealed in: 2 seconds')
    await asyncio.sleep(1)
    await q.edit(content='Bets are in place!\nresults revealed in: 1 second')
    await asyncio.sleep(1)
    e = random.randint(0,36)
    if e == 0 or 2 or 4 or 6 or 8 or 10 or 12 or 14 or 16 or 18 or 20 or 22 or 24 or 26 or 28 or 30 or 32 or 34 or 36:
      if choice == 'even':
        win = int(bet) * 2
        await q.edit(content=f'Number:{e}\nYou win {ctx.author.mention}!!!\n\nYou bet {bet}, and win double your money!!!\n{win}')
      if choice == 'odd':
        await q.edit(content=f'Number:{e}\nSorry {ctx.author.mention}, you lost your bet!')
    elif e == 1 or 3 or 5 or 7 or 9 or 11 or 13 or 15  or 17 or 19 or 21 or 23 or 25 or 27 or 29 or 31 or 33 or 35:
      if choice == 'odd':
        win = int(bet) * 2
        await q.edit(content=f'Number:{e}\nYou win {ctx.author.mention}!!!\n\nYou bet {bet}, and win double your money!!!\n{win}')
      if choice == 'even':
        await q.edit(content=f'Number:{e}\nSorry {ctx.author.mention}, you lost your bet!')
  else:
    await ctx.send(f'sorry {ctx.author.mention}, that is not a valid choice. Valid choices: {choices}')

Solution

  • I found that it is likely because user input might have been for example Odd and not exactly odd. I made it so that the inputs are converted to lowercase.

    I don't know why you listed out each even & odd number, so the program now detects if the number is even or odd, and if it is lesser than 37 (or 36 for odd) for the user input to be valid.

    choices = ['odd','even']
    @slash.slash(description='A staple of gambling')
    async def roulette(ctx,bet:int,choice):
      if choice == int:
        await ctx.send('Sorry, you cannot bet on singular numbers yet. this will be added soon')
      elif any(word in choice for word in choices):
        q = await ctx.send('Bets are in place!\nresults revealed in: 3 seconds')
        await asyncio.sleep(1)
        await q.edit(content='Bets are in place!\nresults revealed in: 2 seconds')
        await asyncio.sleep(1)
        await q.edit(content='Bets are in place!\nresults revealed in: 1 second')
        await asyncio.sleep(1)
        e = random.randint(0,36)
        if (e % 2) == 0: # e is even
          if choice.lower() == 'even': # add choice.lower() to make sure that caps lock doesn't break the program.
            win = int(bet) * 2
            await q.edit(content=f'Number:{e}\nYou win {ctx.author.mention}!!!\n\nYou bet {bet}, and win double your money!!!\n{win}')
          if choice.lower() == 'odd': # add choice.lower() to make sure that caps lock doesn't break the program.
            await q.edit(content=f'Number:{e}\nSorry {ctx.author.mention}, you lost your bet!')
        elif (e % 2) != 0: # e is not even
          if choice.lower() == 'odd': # add choice.lower() to make sure that caps lock doesn't break the program.
            win = int(bet) * 2
            await q.edit(content=f'Number:{e}\nYou win {ctx.author.mention}!!!\n\nYou bet {bet}, and win double your money!!!\n{win}')
          if choice.lower() == 'even': # add choice.lower() to make sure that caps lock doesn't break the program.
            await q.edit(content=f'Number:{e}\nSorry {ctx.author.mention}, you lost your bet!')
      else:
        await ctx.send(f'sorry {ctx.author.mention}, that is not a valid choice. Valid choices: {choices}')