0

I'm currently trying to make a scoreboard for a competition from a text file for a uni project.

Each competitor has a competitor_no, competitor_name, and 3 scores they are all displayed on separate lines in the text file (so each competitors data takes 5 lines) eg:

1
Eliza Ianson
9.19
11.79
21.66
2
Cece Durant
17.03
7.02
17.72

I need to be able to add the 3 scores to get the overall score.I have 50 competitors and need to compare them all to display the top 3 competitors printing the competitors number,name and overall score. Is there a way to assign the line value to a variable?

Obviously I am going to need to compare 5 lines at a time to get the required information for each individual competitor and process the data this is how I have code set in my program.

 file = open('veggies_2014.dat')

    for line in file:

            first_place = []
            second_place = []
            third_place = []

            a = 1 
            b = 2
            c = 3
            d = 4
            e = 5

            if line == a:
                    competitor_no = line
            elif line == b:
                    competitor_name = line
            elif line == c:
                    cucumber = line
            elif line == d:
                    carrot = line
            elif line == e:
                    runner_bean = line

            a += 5
            b += 5
            c += 5
            d += 5
            e += 5

            score = float(cucumber) + float(carrot) + float(runner_bean)
            print(score)

            if score > first:
                    first = score
                    first_place.append(competitor_no)
                    first_place.append(competitor_name)
                    first_place.append(score)
            elif score > second:
                    second = score
                    second_place.append(competitor_no)
                    second_place.append(competitor_name)
                    second_place.append(score)
            elif score > third:
                    third = score
                    third_place.append(competitor_no)
                    third_place.append(competitor_name)
                    third_place.append(score)

    file.close()

    print (first_place)
    print (second_place)
    print (third_place)  

I can get the score if statement to work when I am just dealing with a file containing numbers, but having to include the name is where I seem to be stumbling.

Any suggestions?

  • So your file has 5 lines per competitor? can you give me a sample of data your file has..is it like: line1: c1_no, line2:c1_name, line3:score1, line4:score 2, line5: score 3, line6:c2_no....and so on? – labheshr Mar 03 '16 at 20:27
  • yes its exactly set out like that. Ive added 2 competitors information in now – Brontë Hampton Mar 03 '16 at 20:34

4 Answers4

0

You could do something like this (not tested - but you get the idea): basically , every time count goes to 0, you save the score in a personDict with the person's name as the key....

count = 0
c1_no = None;
personDict = dict()
with open("veggies_2014.dat") as f:
    score = 0
    for line in f:
        if count%5==0:
          if c1_no:
              personDict[c1_no] = score
          c1_no = line
          score = 0
       elif count%5 == 1:
          c1_name = line
       elif count%5 in (2,3,4):
           score += float(line)
       count += 1

 #now do whatever you want with the personDict..you will have something like {Emily:10} assuming Emily's scores were 2,3,5 etc
labheshr
  • 2,858
  • 5
  • 23
  • 34
0

Here is what I came up with:

file_content = '''1
Eliza Ianson
9.19
11.79
21.66
2
Cece Durant
17.03
7.02
17.72
3
Foo Bar
10
9.5
11.2
4
Superman
7.9
12.15
9.75
'''


def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    """ from http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]


users = []
for user_data in chunks(file_content.splitlines(), 5):
    user_id = int(user_data[0])
    user_name = user_data[1]
    scores = list(map(float, user_data[2:]))
    overall_score = sum(scores)

    users.append(dict(
        user_id=user_id,
        user_name=user_name,
        scores=scores,
        overall_score=overall_score
    ))


top_3 = sorted(users, key=lambda x: x['overall_score'], reverse=True)[:3]
DevLounge
  • 8,313
  • 3
  • 31
  • 44
0

Since you know the order in which the information appears in the file, try reading the file in blocks of five lines at a time then process it accordingly. In below implementation I store the information in a 2D list which is then sorted by score, thus you get highest score at top.

data = []
count = 1
with open("veggies_2014.dat") as f:
    line = f.readline()
    while line and int(line) == count:
        score = 0
        entry = []
        entry.append(f.readline())   #read line with c_name
        for i in range(3):
            score += float(f.readline())     #add scores
        entry.append(score)
        data.append(entry)
        line = f.readline()
        count += 1

print(data)
data = sorted(data, key = lambda l:l[1], reverse = True)
print()
print(data)
Prashant
  • 316
  • 3
  • 11
0

The finished code I have used with help from some of the answers. Though I have chosen to separate it into different functions in my application so parts can be reused.

    data = []
    count = 1

    print("")
    print("For 2014 Results please enter: veggies_2014.txt ")
    print("For 2015 Results please enter: veggies_2015.txt")
    print("For 2016 Results please enter: veggies_2016.txt ")
    fname = raw_input("Enter file name:  ")
    with open(fname) as f:
            line = f.readline()
            while line and float(line) == count:
                    score = 0
                    entry = []
                    entry.append(count)
                    entry.append(f.readline().strip())          
                    for i in range(3):
                            score += float(f.readline()) #add scores
                    score = round(score, 2)
                    entry.append(score)
                    data.append(entry)
                    line = f.readline()
                    count += 1

    data = sorted(data, key = lambda l:l[2], reverse = True)
    print("")
    final=[]

    for line in data:
            for i in line:
                    final.append(i)

I added this code to display the scoreboard more cleanly.

    a=0
    b=3
    x=1
    for i in range(0,3):
            place=[]
            place = final[a:b]
            string = " ".join(str(x) for x in place)
            print (str(x) + ". " + string)
            a += 3
            b += 3
            x += 1

    print("")