如何检查某人是否在列表中
How can I check if someone is in a List
Code:
@commands.command()
async def allow(self, ctx, member: discord.Member=None):
if member != None:
if ctx.author.id in f'{allowedID}':
with open('owner.json', 'a') as f:
f.write(member.id)
else:
firstArg = (" ").join(content)
user = bot.get_user(firstArg)
if ctx.author.id in f'{allowedID}':
if user != None:
if user != user.Bot:
with open('owner.json', 'a') as f:
f.write(user.id)
Error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'in <string>' requires string as left operand, not int
我想检查 json 中是否有人,但我总是出现此错误,但不知道如何修复它
你不能检查字符串中的整数,显然它不存在。在您的代码中将 ctx.author.id in f'{allowedID}'
更改为 str(ctx.author.id) in f'{allowedID}'
。
>> 2 in 'name'
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-24283495773c> in <module>
----> 1 2 in 'name'
TypeError: 'in <string>' requires string as left operand, not int
>> str(2) in 'name'
False
如果allowedID
是一个整数列表,
if ctx.author.id in f'{allowedID}':
应替换为
if ctx.author.id in allowedID:
如果是字符串列表,则该行应替换为
if str(ctx.author.id) in allowedID:
你现在正在做的是检查一个整数是否在一个字符串中,这不是 python 允许的(你只能检查一个字符串是否在一个字符串中)。
将 id 转换为字符串在两种情况下都有效(allowedID
是 integers/strings 的列表)但是 if ctx.author.id in allowedID:
在它是整数列表的情况下更好,因为不需要转换.
Code:
@commands.command()
async def allow(self, ctx, member: discord.Member=None):
if member != None:
if ctx.author.id in f'{allowedID}':
with open('owner.json', 'a') as f:
f.write(member.id)
else:
firstArg = (" ").join(content)
user = bot.get_user(firstArg)
if ctx.author.id in f'{allowedID}':
if user != None:
if user != user.Bot:
with open('owner.json', 'a') as f:
f.write(user.id)
Error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'in <string>' requires string as left operand, not int
我想检查 json 中是否有人,但我总是出现此错误,但不知道如何修复它
你不能检查字符串中的整数,显然它不存在。在您的代码中将 ctx.author.id in f'{allowedID}'
更改为 str(ctx.author.id) in f'{allowedID}'
。
>> 2 in 'name'
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-24283495773c> in <module>
----> 1 2 in 'name'
TypeError: 'in <string>' requires string as left operand, not int
>> str(2) in 'name'
False
如果allowedID
是一个整数列表,
if ctx.author.id in f'{allowedID}':
应替换为
if ctx.author.id in allowedID:
如果是字符串列表,则该行应替换为
if str(ctx.author.id) in allowedID:
你现在正在做的是检查一个整数是否在一个字符串中,这不是 python 允许的(你只能检查一个字符串是否在一个字符串中)。
将 id 转换为字符串在两种情况下都有效(allowedID
是 integers/strings 的列表)但是 if ctx.author.id in allowedID:
在它是整数列表的情况下更好,因为不需要转换.