[英]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.我知道elif user == whitelist:
中的两个值是相同的,应该触发 elif 语句,但是代码只是通过 elif 语句并转到 else 语句。 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.当我输入elif user == "562448514349662208":
时,elif 语句确实有效:但是我希望 elif 语句能够从变量中读取。 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我对其他变量进行了一些测试,由于某种原因,当我与变量进行比较时,elif 语句不起作用
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.您正在将字符串与 int 进行比较......它们需要是相同的类型才能检查它们是否相同。
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
.基于content
分配给其中两个分支中的字符串切片(我认为?)的事实,我猜您实际上希望user
也是一个字符串,用于与白名单进行比较和设置content
。 Just remove the int
conversion from the second line.只需从第二行中删除int
转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.