''' DS2000 Spring 2023 - sample code from class Iterating over lists 1. By value for item in lst: ... 2. By position for i in range(len(lst)): ... Goals for today's data: - print all the opponents - how many games did we win despite scoring only one goal? - rig the data to make tottenham look better ''' TOTTENHAM_FILE = "tottenham_results.txt" def main(): # Gather data - initialize three empty lists and read in data # for each game from the file opponents = [] tot_scores = [] opp_scores = [] with open(TOTTENHAM_FILE, "r") as infile: while True: opponent = infile.readline() tot_score = infile.readline() opp_score = infile.readline() # Check to see if at end of file, if so end the loop if opponent == "": break # If we get this far, the variables have legit data opponents.append(opponent.strip()) tot_scores.append(int(tot_score)) opp_scores.append(int(opp_score)) # 1. print out all opponents # List we need - opponents # Iterate by value for opp in opponents: if opp == "Arsenal": print(":(:(:(:(:(") else: print(opp) # 2. How many games did we win despite scoring only one goal? # Lists we need - tot_scores, opp_scores # Iterate by position because it's two lists at once close_wins = 0 for i in range(len(tot_scores)): # Sanity check print print("i =", i) print("tot score", tot_scores[i]) print("opp score", opp_scores[i]) # Check to see if we won by one goal if tot_scores[i] > opp_scores[i] and tot_scores[i] == 1: close_wins += 1 # 3. Add one to all of Tottenham's goals # Lists we need - tot_scores # Iterate by position because we want to modify the list # Sanity check printing print("before...", tot_scores) for i in range(len(tot_scores)): tot_scores[i] += 1 # Sanity check printing print("after...", tot_scores) # Communcation step - print results print("Close wins (we scored only once)...", close_wins) main()