0

I'm trying to change the value of an index but I keep getting the same error and I have no clue how to fix it. Its for a school project so if anyone could help me out that would be much appreciated.

Traceback (most recent call last): list/scorebord/writing.py", line 18, in if naam == spelerlijst["spelers"]: TypeError: list indices must be integers or slices, not str

import json
spelerlijst = []
with open("test1.json") as spelerlijst:
    spelerlijst = json.load(spelerlijst)

speler = input("geef een naam")
spelers = {"speler": speler, "score": 0}
spelerlijst.append(spelers)
print (spelerlijst)
with open ("test1.json", "w") as data:
    json.dump (spelerlijst, data)
print ("Speler {} is aangemaakt.".format(speler))

for spelers in spelerlijst:
    naam = input("welke speler krijgt een punt")
    if naam == spelerlijst["spelers"]:
        spelers["score"] += int(score)

Thanks in advance.

2
  • You cant index an list by a string. If you want to do that, you must do it like this: val = spelerlijst[spelerlijst.index("spelers")] Commented Oct 30, 2017 at 15:00
  • That error is pretty clear about the problem, isn't it? Commented Oct 30, 2017 at 15:06

1 Answer 1

0

the problem you are having is that the thing you are trying to index is a list, not a dict. a list can only be indexed with numbers, starting from 0. if you want to have indexes as strings, use a dict, or as mentioned above, use the index method of the list type (similar to the find method of strings) to extract the index from the list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.