2015-12-17 2 views
0

Вот мой код, в двух словах он импортирует данные из файла csv, считывает его и определяет проценты и оценки зрачков от их двух меток, хранящихся в файле csv. То, что я пытаюсь сделать, это найти и сохранить количество оценок A, B, C, D и не получится, а затем вывести их в конце так, как «Было 2 A прохода» «Было 4 B-передачи», и т. д.Подсчет числа «проходов» в цикле

Заранее благодарим за помощь.

import csv 

with open ("UASHSDDP3efiles.csv", "r") as csvfile: #Reads the csv file and puts the names into a list 
    reader = csv.reader(csvfile) 
    list0 = [] 
    for row in reader: 
     list0.append(row[0]) 

with open ("UASHSDDP3efiles.csv", "r") as csvfile: #Reads the csv file and puts the prelim marks into a list 
    reader = csv.reader(csvfile) 
    list1 = [] 
    for row in reader: 
     list1.append(row[1]) 

with open ("UASHSDDP3efiles.csv", "r") as csvfile: #Reads the csv file and puts the coursework marks into a list 
    reader = csv.reader(csvfile) 
    list2 = [] 
    for row in reader: 
     list2.append(row[2]) 

list3 = [(int(x) + int(y)) for x, y in zip(list1, list2)] #Creates a list with each set of marks added together 

for i in range(len(list3)): #Creates a loop to go through the list 
    totalmark = list3[i] #Takes the first piece of data and calls it totalmarks, on the second loop, it will be the second piece of data and so on... 
    percentage = (totalmark/150) * 100 #Finds the percentage of their total mark out of 150 

    if percentage >= 70: #Checks if they have received an A grade 
     grade = "A" 
    if 60 <= percentage < 70: #Checks if they have received a B grade 
     grade = "B" 
    if 50 <= percentage < 60: #Checks if they have received a C grade 
     grade = "C" 
    if 45 <= percentage < 50: #Checks if they have received a D grade 
     grade = "D" 
    if percentage < 45: #Checks if they haven't received a grade 
     grade = "No grade" 

    roundedpercentage = round(percentage) #Rounds the percentage to the nearest integer 
    print(list0[i],"'s percentage was", roundedpercentage,"%", "and their grade was:", grade) #Prints the pupils name, percentage and grade 

max=max(list3) #Finds the highest mark 
print("The highest mark achieved was:", max) 
min=min(list3) #Finds the lowest mark 
print("The lowest mark achieved was:", min) 

И вот результат:

>>> Alison Brown 's percentage was 71 % and their grade was: A 
Peter Smith 's percentage was 41 % and their grade was: No grade 
Katrina Cunningham 's percentage was 60 % and their grade was: B 
Jason Talbot 's percentage was 40 % and their grade was: No grade 
Shahida Choudry 's percentage was 70 % and their grade was: A 
Ian Li 's percentage was 50 % and their grade was: C 
Petra Carter 's percentage was 39 % and their grade was: No grade 
Hermann Zimmer 's percentage was 69 % and their grade was: B 
Tatiana Krystof 's percentage was 60 % and their grade was: B 
Oliver Hirschbiegal 's percentage was 49 % and their grade was: D 
Lola Portillo 's percentage was 59 % and their grade was: C 
Alberto Maura 's percentage was 69 % and their grade was: B 
Diana Elliot 's percentage was 25 % and their grade was: No grade 
Hilary Clark 's percentage was 49 % and their grade was: D 
Ruksana Cabuk 's percentage was 11 % and their grade was: No grade 
The highest mark achieved was: 106 
The lowest mark achieved was: 17 

ответ

0

Вы можете создать Dict с классов в качестве ключей и значений приращения для каждого класса встречаются. Цикл становится (сделал несколько кодов упрощений для удобства чтения):

# create the dict first 
gradescount = dict() 

for student, totalmark in zip(list0, list3): # iterate directly over the lists 
    percentage = (totalmark/150) * 100 
    if percentage >= 70: 
     grade = "A" 
    elif percentage >= 60: #elif prevents extra tests 
     grade = "B" 
    elif percentage >= 50: 
     grade = "C" 
    elif percentage >= 45: 
     grade = "D" 
    else: 
     grade = "No grade" 

    # Increment appropriate grade counter 
    gradescount[grade] = gradescount.get(grade, 0) + 1 

    roundedpercentage = round(percentage) 
    print("%s's percentage was %d%% and their grade was: %s" % (
      student, 
      roundedpercentage, 
      grade 
    )) 

Затем, в конце сценария:

for grade in ('A', 'B', 'C', 'D'): 
    print("There were %d %s passes" % (gradescount.get(grade, 0), grade)) 
print("There were %d fails" % (gradescount.get('No grade', 0),)) 

В надежде, что это то, что вы хотели.

0

сделать массив из пяти элементов a = np.zeros (5). в каждом классе кода макияжем

if percentage >= 70: grade = "A" ind=ord(grade)-ord("A") a[ind]=a[ind]+1
в CAE из еще т.е. не класса сделать a[4]=a[4]+1. и вы получите массив с нужным ответом

Смежные вопросы