如何修复“类型错误:‘int’和‘str’实例之间不支持‘<’

1 python python-3.x discord.py

我正在尝试制作一个discord.py经济机器人,所以我首先从货币开始。我正在与某人合作制作一个系统,您可以在其中向某人添加硬币并查看您有多少硬币,但我收到了这些错误。

这是针对 Discord.py 经济机器人的。我是重写新手,所以我可能犯了一个愚蠢的错误,但我找不到它。

import discord
from discord.ext import commands
import json
import os

bot = commands.Bot(command_prefix="!")
token = 

def user_add_coins(user_id: int, points: int):
    if os.path.isfile("coins.json"):
        try:
            with open('coins.json', 'r') as fp:
                users = json.load(fp)
            users[user_id]['coins'] += points
            with open('coins.json', 'w') as fp:
                json.dump(users, fp, sort_keys=True, indent=4)
        except KeyError:
            with open('coins.json', 'r') as fp:
                users = json.load(fp)
            users[user_id] = {}
            users[user_id]['coins'] = points
            with open('coins.json', 'w') as fp:
                json.dump(users, fp, sort_keys=True, indent=4)
    else:
        users = {"user_id": {}}
        users[user_id]['coins'] = points
        with open('coins.json', 'w') as fp:
            json.dump(users, fp, sort_keys=True, indent=4)

def get_points(user_id: int):
    if os.path.isfile('coins.json'):
        with open('coins.json', 'r') as fp:
            users = json.load(fp)
        return users[user_id]['coins']
    else:
        return 0

@bot.event
async def on_message(message):
    user_add_coins(message.author.id, 1)

@bot.command()
async def coins(ctx):
    coins = get_points(ctx.author.id)
    await ctx.send(f"Your Coins Is `{coins}` !")

@bot.group()
async def add(ctx):
    if ctx.command_invk is None:
        return

@add.command()
async def coins(ctx, args: int, member: discord.Member):
    if ctx.author.id in owners:
        user_add_coins(member.id, int(args))
        await ctx.send(f"Sussces Add {args} to {member.mention} !")


bot.run(token)
Run Code Online (Sandbox Code Playgroud)

这是错误消息,我似乎无法弄清楚为什么。

Traceback (most recent call last):
  File "C:/Users/MYNAME/PycharmProjects/Faction Discord Bot/Faction Discord Test Bot.py", line 14, in user_add_coins
    users[user_id]['coins'] += points
KeyError: 474744664449089556

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\site-packages\discord\client.py", line 255, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/MYNAME/PycharmProjects/Faction Discord Bot/Faction Discord Test Bot.py", line 40, in on_message
    user_add_coins(message.author.id, 1)
  File "C:/Users/MYNAME/PycharmProjects/Faction Discord Bot/Faction Discord Test Bot.py", line 23, in user_add_coins
    json.dump(users, fp, sort_keys=True, indent=4)
  File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\json\__init__.py", line 179, in dump
    for chunk in iterable:
  File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\json\encoder.py", line 430, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "C:\Users\MYNAME\Anaconda3\envs\tutorial\lib\json\encoder.py", line 353, in _iterencode_dict
    items = sorted(dct.items(), key=lambda kv: kv[0])
TypeError: '<' not supported between instances of 'int' and 'str'
Run Code Online (Sandbox Code Playgroud)

抱歉,代码垃圾邮件。

DYZ*_*DYZ 6

有些用户users有数字键,有些用户有字符串键。通过传递sort_keys=Truedump()您,坚持用户按其键排序,在这种情况下这是不可能的:您不能混合苹果和橙子。解决方案:删除该选项。

json.dump(users, fp, indent=4)
Run Code Online (Sandbox Code Playgroud)

  • 更好的是,修复“users”,以便所有键都是数字;这似乎是基于“def user_add_coins(user_id:int,points:int):”的预期类型。 (6认同)