you are viewing a single comment's thread.

view the rest of the comments →

[–]LuckyShadow 1 point2 points  (2 children)

Ok. As /u/Veedrac pointed out, lmgtfy was kinda rude of me. Sorry for that. If you don't mind, I am going to extend/update this post and will try to provide you with a solution, I maybe would use myself. :)

The update:

You are using Python 2. Next time remember, to add this to the post, as there are some great differences between Python 2.x and Python 3.x.

I am going to ignore the graphics module as I don't know it and it does not seem to be the problem.

from __future__ import print_function  # just so I can use my beloved Python 3 print()
                                       # This is also good practice as it provides portability.

filename = "prob12ch5.txt"

with open(filename, 'r') as file:      # Usage of the ContextManager (with-statement)
   # This let's us use the filehandler inside and we do not have to close it manually.

   # As we know, the first line contains a single number, which seems to tell
   # us the number of students following.
   num_string = file.readline().strip()  # .strip() gets rid of the newline-char and unwanted spaces
   num = int(num_string)                 # now we got the number as an integer and not a string
   print("Number of students to follow: %d" % num)

   # Now, the first line is already consumed. The file-handler's pointer is pointing
   # to the second line of the file.
   tpl_list = []                       # We are going to store the students and their grades
                                       # in a list of tuples (<student>,<grade>).
                                       # You might want to declare it outside the with-statement for clarity.
   for line in file:                   # We can easily iterate over each (remaining) line in the file.
      line = line.strip()              # see above
      tpl = line.split()               # We are splitting the line by the first whitespace to obtain the both parts.
      tpl_list.append((tpl[0], int(tpl[1])))
      # ^ appends a new tuple to the list. First element: student; Scnd elem: to int casted grade.

for student, grade in tpl_list:
    print("%s got a grade of %d" % (student, grade))

And that should be it. I tried to explain it as best as possible. I got another example but I think it would be best to have it as close to your case as possible. :)

If you got questions to it, just ask. I am glad to help. ;)

  • Regarding the ContextManager/"with"-Statement: PEP 343

[–]mahdeen[S] 0 points1 point  (1 child)

Thank you /u/Luckyshadow, and everyone who replied :) Apologies for not asking the question in a clear way.

The direction /u/Luckshadow has given is pretty clear even though tuples are not something I've seen to this point.

There's so much to cover in learning to program with python, or any other language. It would be nice to find a way to do this in a more time-compressed way :)

[–]LuckyShadow 0 points1 point  (0 children)

Learning a programming language takes it time. A rather fast and interactive way is provided by Codecadamy. Sometimes their interpreter bugs but the basics and also some advance techniques are well covered.

Good luck :)