简体   繁体   English

如何在python中修复TypeError

[英]How to fix a TypeError in python

The problem is, whenever I try to run the code it always tells me: "TypeError: list indices must be integers or slices, not str" 问题是,每当我尝试运行代码时,它总是告诉我:“ TypeError:列表索引必须是整数或切片,而不是str”

As you can see in the code, I have already tried to add int() around my bank / wallet amounts. 如您在代码中所见,我已经尝试在银行/钱包金额周围添加int()。

with open("accounts/" + username + ".json") as file:
    json_file = json.load(file)
    money = json_file['money']
    bank = int(money['bank'])
    wallet = int(money['wallet'])
    if wallet >= deposit_amount:
        new_bank = bank + deposit_amount
        new_wallet = wallet - deposit_amount
        money['bank'] = new_bank
        json_file['wallet'] = new_wallet
        dump_file = open("accounts/" + username + ".json", 'w')
        json.dump(json_file, dump_file)
    else:
        print("You do not have enough money for that!")


File "C:/Users/riley/PycharmProjects/BankManager/main.py", line 20, in <module> startup()
File "C:/Users/riley/PycharmProjects/BankManager/main.py", line 12, in startup login()
File "C:\Users\riley\PycharmProjects\BankManager\handlers\AccountHandler.py", line 16, in login set_panel(username)
File "C:\Users\riley\PycharmProjects\BankManager\handlers\PanelHandler.py", line 9, in set_panel start_panel(username)
File "C:\Users\riley\PycharmProjects\BankManager\panels\user.py", line 29, in start_panel bank = int(money['bank'])

I am making a Money System in python so I can later implement this into a Discord Bot. 我正在用python创建Money System,因此以后可以将其实现到Discord Bot中。 I am attempting to make a deposit command where if you say 'deposit' it will ask you how much you want to deposit and go from there. 我试图发出一个存款命令,如果您说“存款”,它将询问您要存款多少,然后从那里去。

I think jason_file is a dictionary of list. 我认为jason_file是list的字典。 Something like: 就像是:

jason_file = {'money':[some list], .... so on} jason_file = {'money':[某些列表],....依此类推}

So, the result of money = json_file['money'] would be: 因此, money = json_file['money']将是:

money = [some list] 金钱= [部分清单]

As you can see, money here is a list. 如您所见,这里有钱。 It won't accept 'bank' as index. 它不会接受“银行”作为索引。 Your type casting to int by int(money['bank']) does not matter. 您通过int(money['bank'])类型强制转换为int没关系。

Can you please print(money[0]) to check if it indeed is a list? 您可以print(money[0])来检查它是否确实是列表吗? You may also check if jason_load is dictionary of lists? 您还可以检查jason_load是否为列表字典?

The error you get means that money['bank'] is not a valid operation because money is a list (and not a dict ). 您得到的错误意味着money['bank']不是有效的操作,因为money是列表(而不是dict )。 Therefore, you can only access its elements through numerical index ( 0 , 1 , 2 , ...) and not through keys ( 'bank' , 'wallet' , etc.) 因此,只能通过数值指数(访问它的元素012 ,...),而不是通过键( 'bank''wallet' ,等)

From the incomplete snippet you provided ( please provide a Minimum, Complete, and Verifiable Example ) I assume that you want your example to be close to the following: 从您提供的不完整代码段( 请提供最小,完整和可验证的示例 )中,我假设您希望您的示例接近以下内容:

>>> import json
>>> json_string = '{"money": {"bank": "a bank", "wallet": 1234.56}}'
>>> json_data = json.loads(json_string)
>>> json_data
{"money": {"bank": "a bank", "wallet": 1234.56}}
>>> type(json_data['money'])
<class 'dict'>
>>> type(json_data['money']['wallet'])
<class 'float'>

As you can see, the json module loads the JSON type properly and leaves us with Python types. 如您所见, json模块正确加载了JSON类型,并为我们保留了Python类型。 You don't need to cast the values (unless there are enclosed in a string such as ...{"wallet": "1234.56"}... ). 您无需强制转换值(除非字符串包含在...{"wallet": "1234.56"}... )之内。

Double check what your json_file contains, print its content or at least the type of the money element print(type(json_file['money'])) . 仔细检查您的json_file包含什么,打印其内容或至少print(type(json_file['money'])) money元素的类型print(type(json_file['money']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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