3

I'm working on this assignment for school and got it almost nearly all done but I'm missing a really small but crucial piece.

So the full assignment is for the user to enter in some values for grades that a student gets into a list, then the user must be able to see the average of a particular subject or a printed out version of all the grades that all the students got for a subject.

My problem is that how do I put something to tell the user that there isn't any values for a particular subject when she selects to see the avg/all values for that subject. I'll show here what I got with a list that already has some values in, so there's no need to go through the whole process of putting them in

cijfers = [ [ 12345, 'wiskunde', 8.9], [ 12345,'elnet', 4.0], [12345, 'python', 8.9], [98761, 'wiskunde', 6.5], [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'wiskunde', 5],[20945, 'elnet', 6.9], [20945, 'python', 4.5], [65489, 'wiskunde', 3.4], [65489, 'elnet', 6.7], [65489, 'python', 10]]

First one stands for student number, second one is the subject and third one is the grade that that particular student got.

so if i put this is to give me all the grades for the subject "wiskunde"

cijfers = [ [ 12345, 'wiskunde', 8.9], [ 12345,'elnet', 4.0], [12345, 'python', 8.9], [98761, 'wiskunde', 6.5], [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'wiskunde', 5],[20945, 'elnet', 6.9], [20945, 'python', 4.5], [65489, 'wiskunde', 3.4], [65489, 'elnet', 6.7], [65489, 'python', 10]]

a = 0
print ('     Tentamencijfers voor: ','Wiskunde', '\n', '========================================')
print ('         studenten# | cijfer')
while (a<len(cijfers)):
    if (cijfers [a][1] == 'wiskunde'):
        print ('          ',cijfers[a][0], '     ',cijfers[a][2])
    a = a + 1

It gives me the follow as an output:

    Tentamencijfers voor:  Wiskunde             #translates to exam grades for: Math
 ========================================
         studenten# | cijfer
           12345       8.9
           98761       6.5
           20945       5
           65489       3.4

Which is it supposed to do, but let's say if there wasn't any values for "wiskunde" so the list would look like this:

cijfers = [  [ 12345,'elnet', 4.0], [12345, 'python', 8.9],  [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'elnet', 6.9], [20945, 'python', 4.5],  [65489, 'elnet', 6.7], [65489, 'python', 10]]

It would give me this as an output:

Tentamencijfers voor:  Wiskunde #translates to exam grades for: Math
 ========================================
         studenten# | cijfer

I know that it shows just that little bit because i wrote the "print" function before the "while" but that's not my question.

So my question is how can I make it give me just a simple "There's no values put in for this particular subject"?

otherwise
  • 37
  • 3

3 Answers3

2

Well, I'm not sure at all if this is what you want, but works

print ('     Tentamencijfers voor: ','Wiskunde', '\n', '========================================')

lines_studient = []
found = False
for entry in cijfers:
    if (entry[1] == 'wiskunde'):
        lines_studient.append('          '+str(entry[0])+ '     '+str(entry[2]))
        found = True

if not found:
    print ('         None studients')
else:
    print ('         studenten# | cijfer')
    for line in lines_studient:
        print(line)

It uses two for loops: the first to look for if exist one element, and the second to print out the lines.

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
fernandezr
  • 660
  • 6
  • 19
2

To strictly answer your question, I would add a counter and change the loop in the following way:

count = 0
for i in cijfers:
    if (cijfers i[1] == 'wiskunde'):
        count += 1 # Same as count = count + 1
        print ('          ',i[0], '     ',i[2])

#Out of the loop:
if count == 0:
    print "There's no values put in for this particular subject"

As data structure, I think that a dictionary will better suit your needs, using as key the student number; check them out!

Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
-2

Try this. You need not to use two for loops here. You can use the comprehension with any() method:

cijfers = [ [ 12345, 'wiskunde', 8.9], [ 12345,'elnet', 4.0], [12345, 'python', 8.9], [98761, 'wiskunde', 6.5], [98761, 'elnet', 7], [98761, 'python', 4.5], [20945, 'wiskunde', 5],[20945, 'elnet', 6.9], [20945, 'python', 4.5], [65489, 'wiskunde', 3.4], [65489, 'elnet', 6.7], [65489, 'python', 10]]

a = 0
print '     Tentamencijfers voor: ','Wiskunde', '\n', '========================================'
print '         studenten# | cijfer'

if any("wiskunde" in sublist for sublist in cijfers):
    while (a<len(cijfers)):
        if (cijfers [a][1] == 'wiskunde'):
            print '          ',cijfers[a][0], '     ',cijfers[a][2]
        a = a + 1
else:
    print "There's no values put in for this particular subject"
Vimalraj Selvam
  • 2,155
  • 3
  • 23
  • 52
  • It would be great if you add comment and down vote, which will help me to work better. Simply down voting will not help anyone and it may lead to account lock. Kindly consider. – Vimalraj Selvam Nov 10 '14 at 17:00
  • I didn't downvote you myself (I just read all the answers) but I hope that those who did will explain to you why they did it – otherwise Nov 10 '14 at 21:44
  • Thank you @otherwise. I didn't mean anyone specific though, but justifying things would make better always. – Vimalraj Selvam Nov 10 '14 at 21:47