List 计数“的数量;通行证;在循环中
这是我的代码,简而言之,它从csv文件导入数据,读取数据,并根据csv文件中存储的两个分数确定学生的百分比和分数。我想做的是找到并存储有多少个A、B、C、D等级和不合格,然后在最后以“有2个A等级”“有4个B等级”等方式输出它们 提前谢谢你的帮助List 计数“的数量;通行证;在循环中,list,loops,csv,python-3.x,import,List,Loops,Csv,Python 3.x,Import,这是我的代码,简而言之,它从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 =
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)
您可以创建一个dict,将等级作为键,并为遇到的每个等级增加值。for循环变为(为了可读性,对代码进行了一些简化): 然后,在脚本末尾:
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),))
希望这就是你想要的。制作一个由五个元素组成的数组a=np.zero(5)。在每一个等级中,制定代码
如果百分比>=70:
等级=“A”
ind=作战需求文件(等级)-作战需求文件(“A”)
a[ind]=a[ind]+1
在cae中的其他情况,即无等级 使
a[4]=a[4]+1
。您将得到一个包含所需答案的数组
# 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),))