[Tutor] Thanks , Now able to do Marks entry and display it using Histogram
Alan Gauld
alan.gauld at freenet.co.uk
Tue Jan 3 13:23:03 CET 2006
Glad you got it working, now here are some picky comments
about style:
> # This program is to see how can we see how many
> students got particular mark
> # And how u print it
An introductory comment is good but in Python a common
way to do that is to use documentation strings, typically
using triple quotes:
"""This program is to see how can we see how many
students got particular mark
And how u print it """
Now if you import your module and do
print modulename.__doc__
or even
help(modulename)
Python will print the doc string for you whioch can be useful and saves
you opening the file in an editor to read the comments. You can do the
same with functions and classes when you get around to using them...
> print " \nFirst Enter the No of Students ... "
> print "\n Then No of marks "
This is kind of redundant since your raw_input promprts tell us what
to do each time.
> # defining empty array array
> array = []
calling something an array is pretty meaningless, it might be better to
call it marks or scores or something else that decribes its purpose
rather than its structure. One of the design goals of a good
programme should be to hide the details of *how* a program
is built from the reader and reveal *why* the program is built
the way it is.. The same applies to comments.
> # defining i
> i = 0
So the comment above is pretty much redundant since we can see
that we are defining i.The more important question is - what is i for?
Its a counter so why not call it that?
> # n is the NO of students which I have to enter the
> marks
> n = int(raw_input("Enter the no: of students : \n"))
So why not call it numStudents or similar, then you wouldn't
need the comment and the following code will be much
more obvious in its meaning.
> # m is the Marks of the students which I have to enter
m? I see no 'm'. But I do see a 'marks' variable which is self evident.
Looks like to anticipated my comment here! :-)
Although being really picky I'd probably suggest mark since it
only holds one mark at a time... marks implies (to me at least)
some kind of collection.
> while i < n:
> marks = int(raw_input("Enter the Marks for the
> students : \n"))
> array.append(marks)
> print "Marks are ", marks
> i = i+1
>
> #print "Array Marks ", array
>
> # Now to display How many students got the same marks
>
> j = 0
Why introduice a new counter? Just use the previous one.
The more names you have in a programme the harder it is
to remember what they are all for - especially if they are
single letter names. You finished with the counter above
so why not just reinitialise it here.
> ##########
> # To Display the histograph of the students Marks
> # 25 is the Max marks
> #
>
> for j in range(25):
> print j,"x" * array.count(j)
> j += 1
Not sure why you increment j since the for loop does
that for you.
BTW. Another approach to this problem would be to
use a dictionary. That way you can count the occurences
as they are added:
"""
This program is to see how can we see how many
students got particular mark
And how u print it
"""
scores = {}
mark = int(raw_input('mark?(-1 to end) '))
while mark != -1:
try: scores[mark] += 1
except KeyError: scores[mark] = 1 # first time for this mark
mark = int(raw_input('mark?(-1 to end) '))
for key in scores: print key,' x' scores[key]
You can tidy that up and avoid the try/except by using the
get() method of a dictionary, but I'll leave that as an
excercise! Hint: use a default value of zero.:-)
HTH,
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list