''' DS2000 Spring 2023 Sample code from class - how many matches have the hot spurs won so far this far? Steps to read from a file using a while loop: * Run forever * Read in one match's worth of lines * Check to see if we're at the end of file * If so, end the loop with break * If not, see if we won! ''' import matplotlib.pyplot as plt HOTSPUR_FILE = "tottenham_results.txt" def main(): # Gather data - initialize a win variable, & read from file wins = 0 draws = 0 with open(HOTSPUR_FILE, "r") as infile: while True: opponent = infile.readline() tot_score = infile.readline() opp_score = infile.readline() # If at end of the file, stop the loop if opponent == "": break # Computation step starts here # If we get here, we are NOT at the end of the file # Data is real! tot_score = int(tot_score) opp_score = int(opp_score) # Sanity-check printing print(tot_score, ":", opp_score) # Did we win? if tot_score > opp_score: # this is the same as wins = wins + 1 wins += 1 elif tot_score == opp_score: draws += 1 # Pick up here when we hit the break print("Wins:", wins) print("Draws:", draws) # Plot wins and draws in a bar chart plt.bar(1, wins) plt.bar(2, draws) main()