On 15/10/12 02:21, Salinas, Erwin d wrote:
HELP! Hi I'm a newbie in Python, I'm having trouble organizing the FILE into the OUTPUT BELOW. I put the file into read, then I put it on a list, and my plan is to put the name and the corresponding grades into a dictionary, using the elements on the list, but when I print the dictionary, it's not giving me the result I wanted. MY CODE SO FAR IS SHOWN BELOW THE OUTPUT.
There are lots of things we could comment on but I'll highlight just a few key ones. Fix them and try again...
filename = open("student_grades_hm_5.txt") infile = filename.read()
This reads the entire file as a single string. You might find file.readlines() more useful in this case.
mix_list = infile.split()
This splits the file based on whitespace. It will ignore commas so that for example your first few items will be split:
['HM3', '80' ',' 'HM2' ',90' ',' 'ID,' '1000123456,HM4,92,Last_name,Rubble'] You can see this if you print the mix_list variable. Notice that you have commas on their own and the last 'item' is a long one. You need to look more closely at how you want split to work. Check the documentation.
print "Name | ID | HM1 | HM2 | HM3 | HM4 | Avg. |" print "______________________________________________________" Student_one = {"Name":mix_list[9],"ID":mix_list[5],"HM1":0,"HM2":mix_list[3], "HM3":mix_list[1],"HM4":mix_list[7]}
This probably gives you the wrong result because of the way you used split(). Also it will only output a single line, but for testing that may all you intended?
But for future reference its always worth providing a sample of the faulty output so that we don;t need to guess. And instead of just saying it's 'not giving me the result I wanted', be more specific about what you perceive as being wrong. For example 'wrong data', 'bad formatting', 'incomplete data' etc
-- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor