【发布时间】:2016-03-23 21:16:58
【问题描述】:
这是我的代码,简而言之,它从 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
【问题讨论】:
标签: list loops csv python-3.x import