0

I have a list of grades ranging from between 1 and 100.

I need to find the following solution in PYTHON

0-10   0
10-20  5 *****
30-40  7 *******
40-50  8 ********

And so on up to 100. 0-10 has not instances in the list. The number and * represent how many grades are in the category within the list.

I have been told to create a list to of the total grades per quantity first and then work o the table later.

I should be using a for loop.

I’ve tried all different codes but I can’t create the list. I can’t seem the get the code to add in the instances without listing the instances individually.

this is what I have so far

data = [90,30,13,67,85,87,50,45,51,72,64,69,59,17,22,23,
        44,25,16,67,85,87,50,45,51,72,59,14,50,55,32,23,
        24,25,37,28,39,30,33,35,40,34,41,43,94,95,98,99,
        44,45,47,48,49,53,61,63,69,75,77,60,83]

total=0
myList=[]
for grade in data:
myList.append(grade>=10)
data.append(0)
for grade in data(len(myList)):
 total=+1
 elif(grade>10 and grade<20): 
 myList.append(len(grade))
 total=+1
 print(myList)

Any help, please. I’m very new at this.

Andreas K.
  • 9,282
  • 3
  • 40
  • 45
  • 1
    Could you post here your attempts? If you have code to show do it and people will be glad to help you. Otherwise I'm afraid that the question is a bit too broad. – Roberto Caboni Jan 11 '20 at 18:15
  • Perhaps a simple `CASE WHEN` statement could address your need. It would be helpful to post your table structure, a data sample and an example of the desired results. – Kate Jan 11 '20 at 18:23

2 Answers2

0

Since you are a complete beginner, here's a detailed solution in Python:

First, create a list with values 0-10, with 0 for grades 0-9, 1-9 for grades 10-99 and 10 for grade 100. To do this we perform floor division // of the grade with 10.

d = []
for grade in data: 
    d.append(grade // 10)

The same can be done using list comprehension:

d = [grade // 10 for grade in data]

Next, count the occurrences of each value in d using the count() method. We can create another list for this (again using list comprehension):

gc = [d.count(i) for i in range(11)]

And finally print the results:

print('0-9'  , gc[0], '*'*gc[0])
print('10-19', gc[1], '*'*gc[1])
print('20-29', gc[2], '*'*gc[2])
print('30-39', gc[3], '*'*gc[3])
...

Output:

0-9 0 
10-19 4 ****
20-29 7 *******
30-39 8 ********
...
Andreas K.
  • 9,282
  • 3
  • 40
  • 45
0

Python is making big strides thanks to its great libraries.

Do not reinvent the wheel, use Counter from the standard library.

from collections import Counter

cnt = Counter(d//10 for d in data)
for k, n in sorted(cnt.items()):
    print(f"{k*10}-{k*10+9} {n} {'*'*n}")

which produces

10-19 4 ****
20-29 7 *******
30-39 8 ********
40-49 11 ***********
50-59 9 *********
60-69 8 ********
70-79 4 ****
80-89 5 *****
90-99 5 *****

Note: to Counter we pass a generator expression, which avoids creating another list unnecessarily

Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • Thank you so much for your help. The information supplied will be very helpful. Just out of curiosity is there a solution to this using for loops and if statements. This is what we were looking at before the exercise was set. So I just assumed this is what I need to use? – Yellowsun99 Jan 12 '20 at 09:39