I am struggling with a discord bot command that I've been working on recently. I am trying to use randint in order to roll for different outcomes but the number stays the same when i use the variable
x = (random.randint(1,100))
@bot.command()
async def eightball(ctx):
await ctx.send(x)
if x == 1:
await ctx.send ("Thats very lucky!:8ball")
else:
if x in range(11,98):
await ctx.send ("Shame on you! The :8ball: says.")
else:
if x == 100:
await ctx.send ("That also happens to be very fortunate :8ball:")
else:
if x in range(2, 10):
await ctx.send ("Closer and closer! the :8ball: gets!")
else:
if x == 99:
await ctx.send ("Oof, :8ball:")
else:
if x == 1:
await ctx.send ("Wow, The :8ball: Is Impressed")`
You need to have your x
be inside the command. In your code, you are only randomly generating x
once. You need to do it everytime someone uses the command.
@bot.command()
async def eightball(ctx):
x = random.randint(1,100)
if x == 1:
msg = "Thats very lucky! :8ball:"
elif x in range(2, 10):
msg = "Closer and closer! the :8ball: gets!"
elif x in range(11, 98):
msg = "Shame on you! The :8ball: says."
elif x == 99:
msg = "Oof, :8ball:"
elif x == 100:
msg = "That also happens to be very fortunate :8ball:"
else:
# can add else here if you wanted to
# but we can't geenerated a number that ends up here so pointless
# leaving for demo purposes
pass
await ctx.send(msg)
I've also simplified your logic a bit - rather than lots of indented if/else statements, we can use if/elif to check other conditions if the first one is true. This makes your code a bit neater and allows adding other conditions a lot easier. I've also ordered the statements in numerical order - makes it easier to read and parse the logic a bit better. Here's a tutorial on conditonal statements (if/else/elif). Here's another on "scope". The latter might help understand your x
problem.
Could also use pattern matching
but that's relatively new so best to stick to if/elif/else
for now.