''' DS2000 Spring 2022 Sample code from class -- reading in Amy S.'s jeopardy winning streak First task: How much total $$ did Amy win? Test case: it should be: $1,384,800 (This includes the last game, where she took home $2,000 for coming in second place.) Second task: Visualize each day's winning in a bar chart. In matplotlib, we need to specify the x-position and height of each bar, like this: plt.bar(x_position, height) In the code below, we use the number of games so far (count) as the x-position, and the amount won in the current game as the height. Plus some colors! ''' import matplotlib.pyplot as plt FILENAME = "schneider.txt" def main(): total_amount = 0 count = 0 # Step one: gather data from the file # We expect the file to have structure: amount, yes/no, amount, yes/no with open(FILENAME, "r") as infile: while True: # Read in one game's worth of data: the amount won, and yes/no amount = infile.readline() final = infile.readline() # If we have emptiness in amoutn or final # we must be at the end of the file, so stop the loop if amount == "" or final == "": break # If we get this far, then amoutn and final # have real values in them # Step two: computations. Update the total amount won # Strip the "\n" off of final, because we like Yes more than Yes\n amount = float(amount) final = final.strip() # Update the total amount won, and the number of games so far # this is the same as total_amount = total_amount + amount total_amount += amount count += 1 # If she got the final question right, change the color color = "gold" if final == "Yes": color = "limegreen" # Step three: make a bar chart for the current amount # plt.bar(x, height) plt.bar(count, amount, color = color) # Step three: communicate about the total amount print("Total amount won: $", total_amount, sep = "") main()