Hi there, I just wanted to ask how I would go about writing a single function to count the frequency of numbers 0-10 including .5(such as 1.5,2.5)

This is what I have done

file = open ('grade.txt', 'r')

s=file.readline()
grade=[]
while s[0:3]!="EOF":
    s=s.replace(' ','')
    lst=s.split(',')
    if lst[0][0]!='I':
         grade.append(float(lst[5]))
         print(lst)
    s=file.readline()

print(grade)

print("Grade of 0.0 -",grade.count(0))
print("Grade of 0.5 -",grade.count(0.5))
print("Grade of 1.0 -",grade.count(1))
print("Grade of 1.5 -",grade.count(1.5))
print("Grade of 2.0 -",grade.count(2))
print("Grade of 2.5 -",grade.count(2.5))
print("Grade of 3.0 -",grade.count(3))
print("Grade of 3.5 -",grade.count(3.5))
print("Grade of 4.0 -",grade.count(4))
print("Grade of 4.5 -",grade.count(4.5))
print("Grade of 5.0 -",grade.count(5))
print("Grade of 5.5 -",grade.count(5.5))
print("Grade of 6.0 -",grade.count(6))
print("Grade of 6.5 -",grade.count(6.5))
print("Grade of 7.0 -",grade.count(7))
print("Grade of 7.5 -",grade.count(7.5))
print("Grade of 8.0 -",grade.count(8))
print("Grade of 8.5 -",grade.count(8.5))
print("Grade of 9.0 -",grade.count(9))
print("Grade of 9.5 -",grade.count(9.5))
print("Grade of 10.0 -",grade.count(10))

but this is what I actually want, but unsure of how to make it work

def count_grade(x):
            grade=[grade]
            for x in range(0,11):
                y = grade.count(x)
                grade.append(y)
                print(y)

print(count_grade)

what structure has the data in the text file?
show some of its data.
btw, range() doesnt work with float.

I think you'll find that, in order to match the grades up with their counts, you'll want to use a dictionary keyed on the grades.

This article has been dead for over six months. Start a new discussion instead.