简体   繁体   中英

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"

As you can see in the code, I have already tried to add int() around my bank / wallet amounts.

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. 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. Something like:

jason_file = {'money':[some list], .... so on}

So, the result of money = json_file['money'] would be:

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.

Can you please print(money[0]) to check if it indeed is a list? You may also check if jason_load is dictionary of lists?

The error you get means that money['bank'] is not a valid operation because money is a list (and not a dict ). Therefore, you can only access its elements through numerical index ( 0 , 1 , 2 , ...) and not through keys ( 'bank' , 'wallet' , etc.)

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. You don't need to cast the values (unless there are enclosed in a string such as ...{"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'])) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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