CS303E Assignment 8

Due: Thursday July 11  by 11 pm

File Name: team_wins.py  (Program name must match this exactly, including case of letters.)

Submit to Assignment 8 on Gradescope via Canvas

Purpose: To practice working with files, variables, conditional logic, loops, and programmer defined functions.

Limitations: You may only use the Python syntax and language features covered in the book chapters 1 - 6. Specifically you may not use lists, sets, dictionaries or any other data structures.

Import the os module. Use the os.path.isfile(string) function, the strip() method on strings, and the format functions or f Strings as necessary.

Review the assignment guidelines.


Write a program that asks a user for a file name and a team name.

The program then scans the file to determine the number of games the team entered by the user won and loss and displays the results.

The first line of the file is a single integer. This value indicates the number of games in the file. Each game consists of 5 lines in the file following the format below.

The easiest way to process the file is to read in the first line and convert it to an int. Then have a for loop that loops for the number of games, reading 5 lines at a time from the file in the for loop.

# Once the file is opened. Assume the variable is in_file.
num_games = int(in_file.readline().strip())
for i in range(num_games):
    # Process one game, the next 5 lines of the file.

The files provided for this assignment are text files. They contains the results of various games for various sports and seasons. Each game in a file is represented with 5 lines in the file. The format of the games are:

<date of game>
<away team name>
<away team score>
<home team name>
<home team score>

So for example, here are the first two games in the wbb12_e.txt file. This file contains the results of the NCAA Women's Basketball 2011 - 2012 season:

2011-10-27
Bemidji St 
68
Jamestown 
81
2011-10-28
La Sierra 
47
Fresno Pacific 
69

In the first game Bemidji St was the away team and Jamestown was the home team. Jamestown won the game 81 to 68.

In the second game La Sierra was the away team and Fresno Pacific was the home team. Fresno Pacific won the game 69 to 47.

Note, for this assignment we won't have any files that allow tie games such as NCAA football prior to 1996 or soccer (aka association football). Each game will have a winner and a loser. Also note there is always an away team and a home team. The file does not contain any games that were played at a neutral location, such as college football bowl games or the NCAA women's basketball tournament games.

Create programmer defined functions to provide structure.  No function, including main shall be more than 25 lines long not including the docstring at the start of the function or the blank lines before and after the function. The suggested solution has 3 functions in addition to main.

Do not use global variables. You are limited to using local variable and parameters. You must have a main function.

Each run of the program asks the user for a single file and single team name.

If the file does not exist a simple message is printed. Use the os.path.isfile(string) function to determine if the file exists or not.

Assume the user types the file name with no path information whatsoever. Only process the file if it is in the current working directory, the same directory the program is running. Recall, you can get the current working directory (where you should place the files when testing your program) with the os.getcwd() function. If you are not sure where to place the data file so the user does not have to type in any path information run the following statement to determine where the program is running. Place the data files in that same directory (folder) on your computer.

print(os.getcwd())

Note, when reading lines from the file with the readline() function, the returned String will contain whitespace including newline characters. Remove the newline as in the following example:

away_team = input_file.readline().strip()


I strongly suggest you plan out your program in advance. Implement a little bit of your program at a time and test the program before moving on.

Here is a file with multiple runs of the program. Given the same inputs and the same seed your output shall match this exactly.

 I strongly recommend you check your output and the expected output with a diff program such as https://www.diffchecker.com/.

Here are the 3 files used in the samples and a very small file you may want to use when testing:

  1. cfb05_s_e.txt (A small file with with 13 games from the 2005 NCAA College Football Season.)
  2. cfb05_e.txt (Most games from the 2005 NCAA College Football Season.)
  3. wbb12_e.txt (Most games from the 2011 - 2012 NCAA Women's basketball season.)
  4. mlb12_e.txt (Most games from the 2012 Major League Baseball season.)

Recall, you must include and fill out the header for the assignment and place it at the top of the program. Replace any items in <> with the proper information and  delete the <>.

# File: team_wins.py
# Description: <A DESCRIPTION OF YOUR PROGRAM>
# Assignment Number: 8
#
# Name: <YOUR NAME>
# EID:  <YOUR EID>
# Email: <YOUR EMAIL>
# Grader: <YOUR GRADER'S NAME Mayra or Risha
#
# On my honor, <YOUR NAME>, this programming assignment is my own work
# and I have not provided this code to any other student.