How to say something when the specific value you are looking for is not in the list

I am working on this assignment for school and have gotten almost everything, but I am missing a very small but important part.

So the full purpose is for the user to enter some values ​​for the grades that the student is listed, then the user should be able to see the average of a particular subject, or a printed version of all grades that all students received the subject.

My problem is how can I tell the user something that there are not any values ​​for a particular item when she selects to view the avg / all values ​​for that item. I will show here what I have with a list that already has some values, so there is no need to go through the whole process to put 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]]

      

The first is the student's number, the second is the subject, and the third is the grade that that particular student received.

so if i put this to give me all the scores for "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 following output:

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

      

What it should do, but let's say that there were no 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]]

      

This will give me this as a result:

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

      

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

So my question is, how can I get him to give me just "There are no values ​​for this particular item"?

+3


source to share


3 answers


To strictly answer your question, I would add a counter and change the loop like this:

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 no values put in for this particular subject"

      



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

+2


source


Well I'm not at all sure if this is what you want, but it 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 loops, the first to find if one item exists, and the second to print strings.

+2


source


Try it. You don't need to use two for the loops here. You can use 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 no values put in for this particular subject"

      

-2


source







All Articles