0

The user is prompted for a file, which in this case is 'histogram.txt'. The program takes each score in the text file and makes a histogram out of all the grades in the file, organizing them so that the user can see how many of each range there are. I wrote out a very simple code:

filename = raw_input('Enter filename of grades: ')
histogram10 = 0
histogram9 = 0
histogram8 = 0
histogram7 = 0
histogram6 = 0
histogram5 = 0
histogram4 = 0
histogram3 = 0
histogram2 = 0
histogram1 = 0
histogram0 = 0
for score in open(filename):
 if score >= 100:
 histogram10 = histogram10 + 1
 elif score >= 90: 
 histogram9 = histogram9 + 1
 elif score >= 80:
 histogram8 = histogram8 + 1
 elif score >= 70:
 histogram7 = histogram7 + 1
 elif score >= 60:
 histogram6 = histogram6 + 1
 elif score >= 50:
 histogram5 = histogram5 + 1
 elif score >= 40:
 histogram4 = histogram4 + 1
 elif score >= 30:
 histogram3 = histogram3 + 1
 elif score >= 20:
 histogram2 = histogram2 + 1
 elif score >= 10:
 histogram1 = histogram1 + 1
 elif score >= 0:
 histogram0 = histogram0 + 1
print 
print 'Grade Distribution'
print '------------------'
print '100 :',('*' * histogram10)
print '90 - 99 :',('*' * histogram9)
print '80 - 89 :',('*' * histogram8)
print '70 - 79 :',('*' * histogram7)
print '60 - 69 :',('*' * histogram6)
print '50 - 59 :',('*' * histogram5)
print '40 - 49 :',('*' * histogram4)
print '30 - 39 :',('*' * histogram3)
print '20 - 29 :',('*' * histogram2)
print '10 - 19 :',('*' * histogram1)
print '00 - 09 :',('*' * histogram0)

however whenever i run the program, all twenty grades get recorded onto the>= 100 like this:

100 : ********************
90-99 : 
80-89 : 

etc. ... How do I make it so that the program puts the stars in the correct places?

asked Mar 29, 2012 at 5:12
5
  • 3
    Always step 1: make sure the data that is getting into the program is correct. Commented Mar 29, 2012 at 5:14
  • 2
    you are comparing string with numbers... Commented Mar 29, 2012 at 5:17
  • 1
    A little tip: Instead of having 11 different variables for the histogram, you could use a list and calculate the index, e.g. histogram[min(100, score) / 10] += 1. The min is to put all scores above 100 in the same slot. For more general data, like counting the number of words in a text, you could use a dictionary. Commented Mar 29, 2012 at 5:22
  • Any number is always less than any string in Python 2. Commented Mar 29, 2012 at 6:38
  • One liner just for fun any(__import__('sys').stdout.write('<= {} \t {}\n'.format(i * 10 + 10, '*' * num)) for i, num in ((k, len(list(g))) for k, g in __import__('itertools').groupby( sorted(int(score.strip()) for score in open(raw_input('Enter filename of grades: '))), key = lambda score: (score - 1) / 10))). Commented Mar 29, 2012 at 6:50

2 Answers 2

4

Data read from a file is a string. Convert it to an integer first by passing it to int().

>>> int('25')
25
answered Mar 29, 2012 at 5:15
Sign up to request clarification or add additional context in comments.

Comments

2

You need to convert score to int before comparing.

score = int(score) # convert to int
if score >= 100:
 histogram10 = histogram10 + 1
# other cases

If you have blank lines in input file then you have to add necessary check before converting to int. Also instead of ten different variables you can easily use a list.

answered Mar 29, 2012 at 5:18

2 Comments

thank you for this i also appreciate the advice. im still very new at compsci :P if you don't mind can you explain how to use a list with this?
Please go through the list tutorial docs.python.org/tutorial/introduction.html#lists. For histogram0 you can use l[0], for histogram1 you can use l[1] and so on.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.