Zenot1c
Zenot1c

Reputation: 25

Search 3D list for single Item

I am creating a leaderboard creating system, where it checks if the Name is already in the Database, if not it adds it, and if it is it replaces the name and score.

import csv
winner =["Player", 100]
def leaderboardsave(winner):
    fileCSV = open('scores.csv')
    dataCSV = csv.reader(fileCSV)
    playersScores = list(dataCSV)
    winnerName = winner[0]
    winner_index = playersScores.find(winnerName)
    if winner_index > -1:
        playersScores[winner_index] = winner
    else:
        playersScores.append(winner)
leaderboardsave(winner)

The CSV is saved like this:

Player, 20
Player2, 40
Player3, 30
Player4, 60

whenever I run

    winner_index = playersScores.find(winnerName)

it returns "'list' object has no attribute 'find'" Any other ways to find where the item is in the list? When i tried using .index, it wouldnt find it, i assume as it is looking for a list, not a single string?

Upvotes: 0

Views: 72

Answers (1)

Kenan
Kenan

Reputation: 14094

You are getting that error because playerScores is a list object and a list object doesn't have a find function.

You can traverse a list to find a value by looping:

winner_index = [index for index, value in enumerate(playerScores) if value == winnerName]

Upvotes: 1

Related Questions