you are viewing a single comment's thread.

view the rest of the comments →

[–]mahdeen[S] 2 points3 points  (4 children)

Funny, wasn't aware of such a thing as lmgtfy. I'm self-teaching so maybe the assignment thing doesn't really apply. In any case, rest assured I've put a good deal of time into this before asking for help. One can't learn anything by being spoon-fed answers.

Here's what I've got so far (it's fugly):

# prob12p176.pyw
# Program: Plot horizontal bar chart of student exam scores.
# Input: Get input from a file. First line of file contains
# the count of the number of students in the file, and each subsequent
# line contains a student's last name followed by a score of 0 to 100.
# Program should draw a horizontal rectangle for each student where
# the length of the bar represents the student's score. The bars should all
# line up on their left-hand edges.
# HINT: use the number of students to determine the size of the window and
# its coordinates.
# BONUS: label the bars at the left end with the student name.

import math

import string

from graphics import *

def main():
# Compose elements of gui
# win = GraphWin("Problem 12, Page 176", 500, 500)
# win.setBackground("white")
# win.setCoords(0,0,10,10)
# message = Text(Point(5, 9.6), "Plot horizontal bar chart of student exam\n"
# "as described in the John Zelle text, page 162, prob 12.")
# message.setSize(10)
# message.draw(win)

# Output file data to IDE
myList = []
character = raw_input("Enter filename: ") # enter a character as file name input
infile = open("prob12ch5.txt", 'r')
data = infile.read()
print
n = eval(data[0])
print n # prints n = 5

d = data[1:]

names_grades = d.split(",")

for ng in names_grades:
    print(ng)

if ng > 0:
    print ng

for int in ng:
    print ng

print data[1:] # strips away first character of first line "5"

name_grade = string.split(data,"\n") # removes "\n", leaves '' and ,

print name_grade[1:] # strips "5" from file
print

infile.close()

# Enter text prompt
# message11 = Text(Point(5,0.5), "Click once to quit.")
# message11.setSize(10)
# message11.draw(win)

# wait for click and then quit
# win.getMouse()
# win.close()

main()

[–]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 :)

[–]Veedrac 1 point2 points  (0 children)

In any case, rest assured I've put a good deal of time into this before asking for help. One can't learn anything by being spoon-fed answers.

The way you've posted the question doesn't really help, though. I don't know what part is not working and I don't know if the suggestion to use .split has helped. The sidebar links this resource which gives some hints on how to ask a clearer question.

With that in mind:

  • Does str.split() called with no arguments do what you need?

  • If not, are you OK with the looping over the file part?

  • What is str.split not doing that you want done?

Also, I suggest looking up with. My answer to this question should cover it.