-1

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])
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
D McKenna
  • 5
  • 2

1 Answers1

0

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.

USFBS
  • 279
  • 3
  • 12
  • Hi, thanks for the reply. I'm an ultra noob with programming of any sort so forgive my ignorance but is there a significance to the {} parenthesis as I haven't seen these in Python before? – D McKenna May 19 '15 at 07:15
  • Yes, the {} signify something as a dictionary. In python, dictionaries allow you to access data values by what are called keys. so if you have `example_dict={"Ray":"Good Cook"}` and you call `example_dict["Ray"]` python will output `"Good Cook"`. It is very useful (you can also store lists to keys, as the code above does) – USFBS May 19 '15 at 12:17
  • You can learn more about dictionaries in python [here](http://www.tutorialspoint.com/python/python_dictionary.htm) – USFBS May 19 '15 at 12:28
  • Hi, thanks for that. Went through a version with nested lists but made it loads more complicated. Got this now – D McKenna May 21 '15 at 11:14