简体   繁体   中英

Elif statement does not appear to be reading variable correctly

I know for a fact that the two values in elif user == whitelist: are the the same and should be triggering the elif statement however the code just passes the elif statement and goes to the else statement. The elif statement does work when I put elif user == "562448514349662208": however I want the elif statement to be able to read from a variable. I did some testing with other variables and for some reason the elif statement just would not work when I was comparing to a variable

Here is more of my code in case you need more context

whitelist = "562448514349662208"
user = int(message.author.id)
if message.author == bot.user:      
    content = message.content[3:]
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.delete()
    await message.channel.send(embed=embedvar)
if user == whitelist:
    content = message.content[3:]
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.channel.send(embed=embedvar)
else: 
    content = user
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.channel.send(embed=embedvar)
whitelist = "562448514349662208"
user = int(message.author.id)
if message.author == bot.user:      
    content = message.content[3:]
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.delete()
    await message.channel.send(embed=embedvar)
if user == int(whitelist):
    content = message.content[3:]
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.channel.send(embed=embedvar)
else: 
    content = user
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.channel.send(embed=embedvar)

You are comparing a string to an int...they need to be the same type to check if they are the same.

Your types are all mismatched:

whitelist = "562448514349662208"   # type: str
user = int(message.author.id)      # type: int  <-- I don't think you want this!
if message.author == bot.user:      
    content = message.content[3:]  # type: str
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.delete()
    await message.channel.send(embed=embedvar)
if user == whitelist:
    content = message.content[3:]  # type: str
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.channel.send(embed=embedvar)
else: 
    content = user                 # type: int
    color = discord.Color(value=int("%06x" % random.randint(0, 0xFFFFFF), 16))
    embedvar = discord.Embed(description=content, color=color)  
    await message.channel.send(embed=embedvar)

Based on the fact that content is assigned to a string slice (I think?) in two of these branches, I'm guessing you actually want user to be a string as well, both for comparison to the whitelist and for setting content . Just remove the int conversion from the second line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM