Problem with map and lists (beginner)

Why is this code not working?

favoriete_sporten = ["Ralf Willems, Voetbal","Michel Tippels, Basketbal"]
print(favoriete_sporten["Ralf Willems"])

Current behaviour:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    print(favoriete_sporten["Ralf Willems"])

TypeError: list indices must be integers or slices, not str

Desired behavior
Voetbal

Repl link:

code snippet

use a dict instead

# Define a dictionary where keys are names and values are sports
favoriete_sporten = {
    "Ralf Willems": "Voetbal",
    "Michel Tippels": "Basketbal"
}

# Now you can directly access the sport by name
print(favoriete_sporten["Ralf Willems"])  # Output: Voetbal

thanks a lot mate!!!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.