You have been asked to create a program to keep track of the scores from the football Premier League.
Your program will store match results in a text file using the following format:
home team;away team;home score;away score;
For instance, after a few games your text file may contain the following information:
Chelsea;Everton;2;0;
Liverpool;Arsenal;4;0;
Crystal Palace;Swansea City;0;2;
Newcastle United;West Ham United;3;0;
Manchester United;Leicester City;2;0;
Manchester City;Everton;1;1;
Swansea City;Manchester United;0;4;
Liverpool;Crystal Palace;1;0;
To complete this challenge you will need to read more about how to read through a CSV file.
Task 1:
Your program should include:
- An option to input and append a new line / match score to the text file. To do so the program will:
- Ask the user to input the name of the Home team,
- Ask the user to input the name of the Away team,
- Ask the user to input the Home Score (number of goals scored by the home team.),
- Ask the user to input the Away Score (number of goals scored by the away team.),
- Append all this information at the end of the text file.
Task 2:
Create another option where the program will:
- Ask the user to display all the scores on screen using the following format:
Home Team – Home Score:Away Score – Away Team
Task 3:
Create a third option where the program will:
- Allow the user to enter a team name to display all the results of this team.
- Calculate and display the number of points of a team; knowing that a team scores 3 points for a win, 1 point for a draw and 0 point for a loss.
Top-Down Modular Design
This diagram represents the main components of our system:
Python Code
We have started the code for you by implementing a basic menu structure. You task is to implement the code for each one of the three options:
Solution
Task 1: View Solution
Check the following
addScore() function and see how you could use it to solve task 1:
|
def addScore(): print("--- Adding New Score ---") homeTeam = input("Type the name of the home team:") awayTeam = input("Type the name of the away team:") homeScore = input("How many goals for " + homeTeam + "?") awayScore = input("How many goals for " + awayTeam + "?") #Append data at the end of the text file file = open("results.txt","a") file.write("\n" + homeTeam + ";" + awayTeam + ";" + homeScore + ";" + awayScore + ";") file.close() print("Your match score has now been added.") |
Task 2: View Solution
Check the following
displayResults() function and see how you could use it to solve task 2:
|
def displayResults(): #Output all scores stored in the text file print("--- All Scores ---") file = open("results.txt","r") for line in file: data = line.split(";") homeTeam = data[0] awayTeam = data[1] homeScore = data[2] awayScore = data[3] print(homeTeam + " - " + homeScore + ":" + awayScore + " - " + awayTeam) file.close() |
Task 3.a: View Solution
Check the following
displayTeamResults() function used to perform a
linear search on the text file to find all the scores of a given team (task 3 part a):
|
def displayTeamResults(): print("--- Team Scores ---") team = input("Type the name of a team:") #Display all the scores for the given team using a linear search file = open("results.txt","r") for line in file: data = line.split(";") homeTeam = data[0] awayTeam = data[1] if team == homeTeam or team==awayTeam: homeScore = data[2] awayScore = data[3] print(homeTeam + " - " + homeScore + ":" + awayScore + " - " + awayTeam) file.close() |
Task 3.b: View Solution
Check the following
displayTeamPoints() function used to perform a
linear search on the text file to find all the scores of a given team and calculate the total league table points for this team (task 3 part b):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
def displayTeamPoints(): print("--- Team Scores ---") team = input("Type the name of a team:") points = 0 numberOfGamesPlayed = 0 #Display all the scores for the given team using a linear search file = open("results.txt","r") for line in file: data = line.split(";") homeTeam = data[0] awayTeam = data[1] if team == homeTeam or team==awayTeam: numberOfGamesPlayed += 1 homeScore = int(data[2]) awayScore = int(data[3]) print(homeTeam + " - " + str(homeScore) + ":" + str(awayScore) + " - " + awayTeam) if homeScore == awayScore: #It's a draw points += 1 elif homeScore>awayScore and team == homeTeam: #It's a win at home! points += 3 elif awayScore>homeScore and team == awayTeam: #It's a win against the home team! points += 3 file.close() print(team + " played " + str(numberOfGamesPlayed) + " game(s).") print(team + " has " + str(points) + " point(s) so far.") |

Solution...
The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area