这里是我的代码,简而言之,它从csv文件导入数据,读取它并确定学生在csv文件中存储的两个标记的百分比和等级。我想要做的是找到并存储有多少A,B,C,D等级并且失败,然后以类似于&#34的方式输出它们;有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 :(得分:0)
您可以创建一个以成绩作为键的词典,并为遇到的每个成绩增加值。 for循环变为(为了便于阅读,进行了一些代码简化):
# 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),))
希望这是你想要的。
答案 1 :(得分:0)
制作一个包含五个元素的数组a = np.zeros(5)。在每个年级制作代码
if percentage >= 70:
grade = "A"
ind=ord(grade)-ord("A")
a[ind]=a[ind]+1
在cae of else,即没有等级
制作a[4]=a[4]+1
。你会得到一个有所需答案的数组