""" rectangles.py chapter 5 homework, from Zelle's textbook The student data is in a file that looks like this : --- data.txt --- Jim,30 Joe,100 Jane,20 Mary,50 Sam,40 -------------- Jim Mahoney | cs.marlboro.edu """ from graphics import * ################# # 0. Create a window and setup a cordinate system. ## The x axis will hold 5 names, so I'll set coords 0 to 6 ## with (1,2,3,4,5) for rectangle positions ## The y axis I'll choose to go from 0 to 110. ## with coords from -10 to 120 window = GraphWin("rectangle homework", 400, 300) window.setCoords(0, -10, 6, 120) ################################################### # 1. Read in the student data from a file. # (Or for testing, just define them like this : # names = ['Jim', 'Joe', 'Jane', 'Mary', 'Sam'] # scores = [30, 100, 20, 50, 40] file = open("data.txt") lines = file.readlines() names = [] # start with empty lists scores = [] # to initialize accumulator loop for line in lines: (name, score) = line.split(',') # print " name is '" + str(name) + "'" # print " score is '" + str(score) + "'" # print names = names + [name] # adding lists scores = scores + [int(score)] # makes a longer list # print "names = " + str(names) # print "scores = " + str(scores) ############################################# # 2. Plot the data in the graphics window, # using code similar to Zelle's interest GUI program # in the textbook. (The numbers in the text positioning # were found with a bit of trial and error.) for i in range(5): name = names[i] score = scores[i] rectangle = Rectangle(Point(i + 1, 0), Point(i + 2, score)) rectangle.draw(window) label = Text(Point(i + 1.3, -5), name) label.draw(window) ################################################## # 3. Wait for the user to stop. # (And just for kicks, get a mouse click and show its coords.) print "Click somewhere" click = window.getMouse() print "You clicked at (" + str(click.x) + "," + str(click.y) + ")" wait = raw_input("OK? ")