Question
Project in PythonWrite a program, world_cup.py , which reads in the results of the games in a particular group in the world cup in soccer
Project in PythonWrite a program, world_cup.py, which reads in the results of the games in a particular group in the world cup in soccer and prints the results along with the table of standings (points, goals and goal difference) in the group. An example input file, groupA.txt,
isQatar;Ecuador;0;2
Senegal;Netherlands;0;2
Netherlands;Ecuador;1;1
Qatar;Senegal;1;3
Ecuador;Senegal;1;2
Netherlands;Qatar;2;0
Here can be seen, e.g., that Ecuador won Qatar 2-0, Netherlands and Ecuador made a draw 1-1, and Netherlands won Qatar 2-0.
The program starts by displaying the results in the following manner:
Enter filename: groupA.txt
Qatar 0:2 Ecuador
Senegal 0:2 Netherlands
Netherlands 1:1 Ecuador
Qatar 1:3 Senegal
Ecuador 1:2 Senegal
Netherlands 2:0 Qatar
Here, the first line is input. In the output that follows, the name of the first team is printed in a right-justified field of width 13.
When the results have been shown, the program asks if the table of standings should also been displayed. If the user does not answer with "y", then the program does not display anything more, but if the answer is "y", the table of standings is shown in the following manner:
Here, the first line is input. In the output that follows, the name of the first team is printed in a right-justified field of width 13.
When the results have been shown, the program asks if the table of standings should also been displayed. If the user does not answer with "y", then the program does not display anything more, but if the answer is "y", the table of standings is shown in the following manner:
Show standings? (y/n): y
Netherlands 7 5:1 4
Senegal 6 5:4 1
Ecuador 4 4:3 1
Qatar 0 1:7 -6
Three points are awarded for a win, one point for a draw, and 0 points for a loss. Here, it can be seen that Netherlands has seven points (2 wins and one draw), has scored five goals and conceded one, and its goal difference is four goals. The name of the team is printed in a left-justified field of width 13, the points in a right-justified field of width 2, the goals (x:y) in a right justified field of width 6, and the goal difference in a right-justified field of width 3. The input and output given the input file groupA.txt is then:
Enter filename: groupA.txt
Qatar 0:2 Ecuador
Senegal 0:2 Netherlands
Netherlands 1:1 Ecuador
Qatar 1:3 Senegal
Ecuador 1:2 Senegal
Netherlands 2:0 Qatar
Show standings? (y/n): y
Netherlands 7 5:1 4
Senegal 6 5:4 1
Ecuador 4 4:3 1
Qatar 0 1:7 -6
COL_SEPARATOR = ';' # separator between fields in input file
# Width of fields in the output
TEAM_WIDTH = 13
POINTS_WIDTH = 2
GOAL_DIFF_WIDTH = 3
SCORED_CONCEDED_WIDTH = 6
# Points for win, draw and loss
POINTS_FOR_WIN = 3
POINTS_FOR_DRAW = 1
POINTS_FOR_LOSS = 0
def main():
"""Reads the input data and produces the output."""
filename = input("Enter filename: ")
file_stream = open_file(filename)
if file_stream is None:
print(f"File {filename} not found!")
return
results_list = get_results(file_stream)
file_stream.close()
print_results(results_list)
show_standings = input("Show standings? (y/n): ")
if show_standings == 'y':
standings_dict = generate_standings(results_list)
print_standings(standings_dict)
def open_file(filename):
"""Opens the file with the given file name.
Returns the corresponding file stream, or None if the file cannot be opened.
"""
try:
file_stream = open(filename)
return file_stream
except FileNotFoundError:
return None
def get_results(file_stream):
"""Reads the given file stream and returns the contents in a list.
Each entry in the list is a tuple: (team1, team2, goals_team1, goals_team2).
"""
pass
def print_results(results_list):
"""Prints each individual result, nicely formatted."""
pass
def generate_standings(results_list):
"""Creates and returns a dictionary with information on standings
giving the results in each game.
The name of a team is a key in the dictonary.
The value is a list containing accumulated points, goals scored and goals conceded.
"""
pass
def print_standings(standings_dict):
"""Prints the table of standings for the teams in the group,
sorted by points and then by goal difference.
"""
# First, compute and store the goal difference for each team
for team in standings_dict.keys():
[points, scored, conceded] = standings_dict[team]
standings_dict[team] = [points, scored, conceded, scored-conceded]
# Second, sort by goal difference and then by points
# sorted_list is a list of (team, [points, scored, conceded, scored-conceded])
sorted_list = sorted(standings_dict.items(), key=lambda item: item[1][3], reverse=True)
sorted_list = sorted(sorted_list, key=lambda item: item[1][0], reverse=True)
print()
# Here you need to print out the standings table in a for-loop
pass
if __name__ == "__main__":
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started