Search code examples
pythonmultidimensional-arrayfile-writing

How do I store a string (name) against 3 corresponding integer values (scores)


I'm trying to write a program that will store the results from a quiz against a player's name. I need to always have a record of the past 3 attempts against the users name. I need to be able to store these results for an indeterminate number of players across 3 class groups (hence the 3 arrays). So far i've got this but am getting pretty stuck now.

I've got 3 arrays with 3 fields. The first field is intended for the name and the following 3 to store the score attempts.

cla_1results = ([],[],[],[])
cla_2results = ([],[],[],[])
cla_3results = ([],[],[],[])

file = open("results.txt"")

if statement determines which array to store the results data in depending on the class code

if class_code == "CL1":                
    cla_1results[0].append(full_name)
    cla_1results[1].append(total)
    file.write([cla_1results])
elif class_code == "CL2":
    cla_2results[0].append(full_name)
    cla_2results[1].append(total)
    file.write([cla_2results])
elif class_code == "CL3":
    cla_3results[0].append(full_name)
    cla_3results[1].append(total)
    file.write([cla_3results])

Solution

  • As far as the structure of storing the scores goes, try using something that looks more like this

    cla_1results={}
    cla_2results={}
    cla_3results={}
    
    my_file=open("results.txt"."r+")
    
    if class_code=="CL1":
        if full_name in cla_1results:
            cla_1results.append(total)
        else:
            cla_1results[full_name]=[total]
        my_file.write(cla_1results[full_name])
    elif class_code=="CL2":
        if full_name in cla_2results:
            cla_2results.append(total)
        else:
            cla_2results[full_name]=[total]
        my_file.write(cla_2results[full_name])
    elif class_code=="CL3":
        if full_name in cla_3results:
            cla_3results.append(total)
        else:
            cla_3results[full_name]=[total]
        my_file.write(cla_3results[full_name])
    

    Also, if you wanted to remove the oldest core if the person had more than 3, you could add in a bit that has cla_1results[full_name][0].remove. Hopefully this helps.