HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Calculating scores for predictions of football scores

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
predictionscalculatingfootballforscores

Problem

I am writing a program which calculates the scores for participants of a small "Football Score Prediction" game.

Rules are:

  • if the match result(win/loss/draw) is predicted correctly: 1 point



  • if the match score is predicted correctly(exact score): 3 points (and +1 point because 1st rule is automatically satisfied)



Score is calculated after every round of matches (10 matches in a round).

The program I've written is as follows:

```
import numpy as np
import re
players = {0:'Alex',1:'Charlton',2:'Vineet'}
results = np.array(range(40),dtype='a20').reshape(4,10)
eachPrediction = np.array(range(20),dtype='a2').reshape(2,10)
score = np.zeros(3)
correctScore=3
correctResult=1
allPredictions = []
done = False

def takeFixtures():
filename='bplinput.txt'
file = open(filename)
text = file.readlines()
fixtures = [line.strip() for line in text]
file.close()
i=0
for eachMatch in fixtures:
x=re.match("(.) ([0-9]+) ?- ?([0-9]+) (.)", eachMatch)
results[0,i]=x.group(1)
results[1,i]=x.group(2)
results[2,i]=x.group(3)
results[3,i]=x.group(4)
i+=1

def takePredictions(noOfParticipants):
for i in range(0,noOfParticipants):
print("Enter predictions by "+players[i]+" in x-y format")
for i in range(0,10):
eachFixturePrediction = raw_input("Enter prediction for "+results[0,i]+" vs "+results[3,i]+": ")
x=eachFixturePrediction.split('-')
eachPrediction[0,i]=str(x[0])
eachPrediction[1,i]=str(x[1])
allPredictions.append(eachPrediction)

def scoreEngine():
for i in range(0,len(players)):
for j in range(0,10):
resultH=int(results[1,j])
resultA=int(results[2,j])
result=resultH-resultA
predictionH=int(allPredictions[i][0][j])
predictionA=int(allPredictions[i][1][j])
pResult = predictionH-predictionA
if result == pResult or (result0 and pResult>

Solution

Style

The first thing to do is to follow PEP 8: it's a style guide that says how you should indent your code, name your variables, and so on. For example, prefer results[0, i] = x.group(1) to results[0,i]=x.group(1).

Files

Opening files in Python should be done using the with idiom: your file is then guaranteed to be close correctly. takeFixtures now becomes:

def takeFixtures():
    with open('bplinput.txt') as file:
        for eachMath in file:
            x=re.match("(.*) ([0-9]+) ?- ?([0-9]+) (.*)", eachMatch.strip)
            results[0,i]=x.group(1)
            results[1,i]=x.group(2)
            results[2,i]=x.group(3)
            results[3,i]=x.group(4)
            i+=1


Data structures

I you didn't use numpy, you could have written results[i] = x.groups(), and then used zip(*results) to transpose the resulting matrix. More generally, as pointed out by Josay, you should use Python data structures, and only switch to numpy if you find out that this is where the inefficiency lies.

Formatting

The last thing that the other reviewers didn't mention is that concatenating strings is poor style, and format() should be preferred since it's more readable and more efficient:

for player in players:
    print("{} has scored {} points".format(players[player], score[player]))

Code Snippets

def takeFixtures():
    with open('bplinput.txt') as file:
        for eachMath in file:
            x=re.match("(.*) ([0-9]+) ?- ?([0-9]+) (.*)", eachMatch.strip)
            results[0,i]=x.group(1)
            results[1,i]=x.group(2)
            results[2,i]=x.group(3)
            results[3,i]=x.group(4)
            i+=1
for player in players:
    print("{} has scored {} points".format(players[player], score[player]))

Context

StackExchange Code Review Q#21408, answer score: 6

Revisions (0)

No revisions yet.