如何将对象保存到JSON列表?

时间:2020-03-05 19:03:02

标签: python json python-3.x save

我正在使用Python3进行文本角色扮演游戏,但是在保存对象时遇到了问题。

我能够保存字符串和整数(例如代表角色名称,统计信息等),但是由于出现错误,所以无法保存对象(例如物品或武器):

write() argument must be str, not Weapon

这是我的代码:

elif userinp == 'save':
    save_place = open("rpgsave.txt", "w")
    list_to_write = [strength, agility, dexterity, wisdom, intelligence, charisma, 
                     main_character.name, main_character.weapons, 
                     main_character.mp, main_character.xp, level_counter, job, 
                     free_points, copcoin, cloc.xpos, cloc.ypos, 
                     main_character.health, inventory]
    for i in range(len(list_to_write)):
        unacceptables = [main_character.weapons, inventory]
        if list_to_write[i] in unacceptables:
            if list_to_write[i] == main_character.weapons:
                save_place.write(list_to_write[i])
                save_place.write("\n")
            elif list_to_write[i] == inventory:
                objects = (Weapon, Item)
                for x in range(len(inventory)):
                    save_place.write(inventory[x])
                    save_place.write("\n")
        else:
            list_to_write[i] = str(list_to_write[i])
            save_place.write(list_to_write[i])
            save_place.write("\n")

0 个答案:

没有答案