So im stuck writing a function and was hoping someone would be nice enough to help. My objective is to look at a list of grades

such as:

``[23 , 64 ,74 , 53 , 55``

And return a list with the number of grades in each percentage range
I.E. 0 - 10 , 10 - 20 , 20 - 30.

So my output from my code above would be something like:

``[0 , 0 , 1 , 0 , 0 , 2 , 1 , 1 , 0 , 0 , 0``

Anyone help is appreciated thanks!

Did you check really the code snippets? I did post http://www.daniweb.com/software-development/python/code/373120.

What kind of error are you getting?

``````grade=dict()
for d in 23 , 64 ,74 , 53 , 55:
grade[d // 10] = grade.setdefault(d // 10, 0) + 1

for mark in grade.items():
print(mark)``````

You would use list instead of dictionary in your case, I leave it …

## All 7 Replies

Thank you for your post but I still could not get a function to compute this list. Or maybe it is I do not understand the post you made?

What kind of error are you getting?

``````grade=dict()
for d in 23 , 64 ,74 , 53 , 55:
grade[d // 10] = grade.setdefault(d // 10, 0) + 1

for mark in grade.items():
print(mark)``````

You would use list instead of dictionary in your case, I leave it up to you to do the adaption.

No errors as of yet as im still trying to construct my code (kind of a newb)

so far all I have is my function def.

``def gradDistribution(examScores):``

I am looking to be able to create this function that reads in numbers (from the shell) and produces the amount of grades in each percentage range.

If you can, you should use standard PEP8 convention names, but you should understand A Foolish Consistency is the Hobgoblin of Little Minds ;) paragraph well also.

So you would define:

``def grade_distribution(examination_scores):``

full words, _ between words.

You get sometimes yourself mixed up with functions and variables, so it is good to start to prefer verb for function:

``def make_grade_distribution(examination_scores):``

Thank you, ill keep that in mind. As far as the rest of the code goes, what should I know? Im really desperate for help as my tutor and friends are busy.

There are two ways to do this.
1. Use a list of lists. The inner list contains the cut off grade and an int which is incremented each time a grade is found in each category. You can loop through the grades once and find the first grade in the inner list that is greater or equal to the actual grade and add one to the counter. So if you want to test for grades in the 50, 70, and 100 ranges, the initial list would contain [[50, 0], [70,0], [100, 0]] and each "0" would be incremented each time a grade is found in that particular range.

2. Pass a list and the particular target grade to a function. The function will iterate through the list and increment a variable/counter for each grade that is less than or equal to the grade passed to the function, otherwise the grade will be added to a new list. The counter and the new list will be returned from the function so the next target grade can be tested.

If you do not know how to do either of these then you do not know enough about Python to solve this problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.