python - Count the elements in a list that matches certain conditions -


i have list, say:

   list = ['kaye', 'david', 'music', '88', 'art', '45', 'french', '36'] 

i want count how many marks less 50 in list, wrote:

count = 0 if  list[3] < '50' or list[5] < '50' or list[7] < '50':     count = count + 1 

but count 1, doesn't accumulate when there more 1 marks less 50.

how can fix this? lot helping

you running 1 test, looks @ multiple possible conditions , executes 1 piece of code if of them true.

i think want count how many of marks <50, , report total.

let's simple version, , grow there:

student = ['kaye', 'david', 'music', '88', 'art', '45', 'french', '36'] alarm = 50  num_alarms = 0  if int(student[3]) < alarm:     num_alarms += 1     print("alarm! " + student[2] + " grade low!")  if int(student[5]) < alarm:     num_alarms += 1     print("alarm! " + student[4] + " grade low!")  if int(student[7]) < alarm:     num_alarms += 1     print("alarm! " + student[6] + " grade low!")  if num_alarms != 0:     print("there " + str(num_alarms) + " grades low.") 

that think you're trying do. can clean bit:

student = ['kaye', 'david', 'music', '88', 'art', '45', 'french', '36'] alarm = 50  num_alarms = 0  # count 3 .. 2 score in range(3,len(student), 2):     if int(student[score]) < alarm:         print("alarm! " + student[score-1] + " grade low!")         num_alarms += 1  if num_alarms != 0:     print("there " + str(num_alarms) + " grades low.") 

finally, can go "full python" , add list comprehensions:

alarms = [ student[class] class in range(2, len(student), 2) if int(student[class + 1]) < alarm ]  class in alarms:     print("alarm! " + class + " grade low!")  num_alarms = len(alarms)  if num_alarms != 0:     print("there " + str(num_alarms) + " grades low.") 

Comments